How to trigger a flow card from a "custom capability" when it changes value

Hi!

I am making a Homey plugin that works for one of my projects for a Solar Controller. I am trying to when the “value” of a custom capability is changed, I want to trigger the flow cards. But I have no idea how to do that, and I cannot find enough examples or documentation for it, is there anyone who can give me a push :smiley:

So what I have is some capabilities which get a new value about every minute:

setSolarHistoryCapabilites(args, jsonData) {
        args.log("Updating solar capabilities");

        try {
            args.setCapabilityValue('real_time_solar', jsonData.realtimeSolar);
            args.setCapabilityValue('measure_battery', parseInt(jsonData.batteryCapacity));
            args.setCapabilityValue('total_load_active_power', jsonData.totalLoadActivePower);
            args.setCapabilityValue('battery_charging', parseFloat(parseFloat(jsonData.batteryCharge.slice(0, -2)) - parseFloat(jsonData.batteryDischarge.slice(0, -2))) + "kWh");    
        } catch(error) {
            args.error("Failed to set solar capabilities", error);
        }
    }

And I have some flow cards that I would like to execute on certain of theese capabilities, here are my flow cards:

{
  "conditions": [
    {
      "id": "battery-more-than",
      "title": {
        "en": "Batteriet blir större än",
        "sv": "Battery is more than"
      },
      "titleFormatted": { 
        "en": "Battery is more than [[kWh]] kWh", 
        "sv": "Batteriet blir större än [[kWh]] kWh" 
      },
      "args": [
        {
          "type": "number",
          "name": "kWh",
          "title": { "en": "Number", "sv": "Nummer" },
          "placeholder": { "en": "5kWh" }
        }
      ]
    },
    {
      "id": "battery-less-than",
      "title": {
        "en": "Batteriet blir mindre än",
        "sv": "Battery is less than"
      },
      "titleFormatted": { 
        "en": "Battery is less than [[kWh]] kWh", 
        "sv": "Batteriet blir mindre än [[kWh]] kWh" 
      },
      "args": [
        {
          "type": "number",
          "name": "kWh",
          "title": { 
            "en": "Number", 
            "sv": "Nummer" 
          },
          "placeholder": { "en": "5kWh" }
        }
      ]
    },
    {
      "id": "solar-generating",
      "title": {
        "en": "Solar panels are generating more than",
        "sv": "Solpanelerna genererar mer än"
      },
      "titleFormatted": { 
        "en": "Solar panels are generating more than [[kWh]] kWh",
        "sv": "Solpanelerna genererar mer än [[kWh]] kWh"
      },
      "args": [
        {
          "type": "number",
          "name": "kWh",
          "title": { 
            "en": "Number", 
            "sv": "Nummer" 
          },
          "placeholder": { "en": "5kWh" }
        }
      ]
    },
    {
      "id": "current-charging-more-than",
      "title": {
        "en": "Battery is charging more than",
        "sv": "Batteriet laddar mer än"
      },
      "titleFormatted": { 
        "en": "Battery is charging more than [[kWh]] kWh", 
        "sv": "Batteriet laddar mer än [[kWh]] kWh" 
      },
      "args": [
        {
          "type": "number",
          "name": "kWh",
          "title": { "en": "Number", "sv": "Nummer" },
          "placeholder": { "en": "5kWh" }
        }
      ]
    }
  ],
  "actions": [
    {
      "id": "buy-mode",
      "title": {
        "en": "Set buy mode",
        "sv": "Sätt köp läge"
      }
    },
    {
      "id": "sell-mode",
      "title": {
        "en": "Set sell mode",
        "sv": "Sätt sälj läge"
      }
    },
    {
      "id": "self-sufficient-mode",
      "title": {
        "en": "Set self sufficient mode",
        "sv": "Sätt självförsörjande läge"
      }
    },
    {
      "id": "pause-mode",
      "title": {
        "en": "Set pause mode",
        "sv": "Sätt pause läge"
      }
    }
  ],
  "triggers": [
    {
      "id": "solar-generating",
      "title": {
        "en": "Solar panels are generating more than",
        "sv": "Solpanelerna genererar mer än"
      },
      "titleFormatted": { 
        "en": "Solar panels are generating more than [[kWh]] kWh",
        "sv": "Solpanelerna genererar mer än [[kWh]] kWh"
      },
      "args": [
        {
          "type": "number",
          "name": "kWh",
          "title": { "en": "Number", "sv": "Nummer" },
          "placeholder": { "en": "5kWh" }
        }
      ]
    },
    {
      "id": "current-charging-more-than",
      "title": {
        "en": "Battery is charging more than",
        "sv": "Batteriet laddar mer än"
      },
      "titleFormatted": { 
        "en": "Battery is charging more than [[kWh]] kWh", 
        "sv": "Batteriet laddar mer än [[kWh]] kWh" 
      },
      "args": [
        {
          "type": "number",
          "name": "kWh",
          "title": { "en": "Number", "sv": "Nummer" },
          "placeholder": { "en": "5kWh" }
        }
      ]
    }
 ] 
}


I have a method where I want to “register” all of the eventListeners kind of but I dont know how to listen to theese capabilites:

activateFlowCards(args) {
        args.log("Activate flow cards");
        
        args.registerCapabilityListener('real_time_solar', (value) => {
            args.log('Changes to :', value);
        });

        args.registerCapabilityListener('measure_battery', (value) => {
            args.log('Changes to :', value);
        });

        args.registerCapabilityListener('total_load_active_power', (value) => {
            args.log('Changes to :', value);
        });
    }

The code is here if you want to look at everything: https://github.com/Hickori/TrestSolarController-Homey/tree/master

Thanks so much in advance!
Best regards Max

Really?

Ah right! My bad, I was there a couple of days ago… Thanks!

How can I register a listener on my capabilites? Theese doesnt seem to trigger:

args.registerCapabilityListener('real_time_solar', (value) => {
            args.log('Changes to :', value);
        });

Found this and it seems to do the trick!

triggerCapabilityListener
1 Like