Trigger flow from entity's state attribute

I’m wanting to use a change in state attribute as a trigger for a flow. For instance, I have a yaml automation for my wemo insight switch that triggers based on the current power consumption. I have it coded as such:

trigger:

platform: numeric_state

entity_id: switch.washing_machine_

value_template: '{{ state.attributes.current_power_w }}'

above: 5

How do I do the equivalent in Node-Red? The Events: State node doesn’t seem to do that because the state for my switch remains “on” all the time.

I think you can use “Trigger: state” node for that.

Does this look correct?
trigger%20node

Can anyone else suggest how to accomplish what I’m trying to do? I fiddled with the Trigger State Node using various Constraints and had them all tied to debugs which never gave me any meaningful output…

did you set the 5 as a number?
image

I think you can make it work using the { } get template node. If you haven’t figured it out let me know I can maybe help you. I’ve got a test going using this node that is extracting an attribute we’d just need to figure out how to trigger it properly for you.

The tool tips on the get template node are too vague for someone rather json-illiterate as myself to figure out. I’m not sure how it would be triggered. Here are the state attributes generated by my Wemo Insight:
Capture

I just need that current_power_w attribute to trigger a flow when it’s above a certain value, e.g. 5

The trick here is going to be creating a trigger that works, we could set something up inside Node-Red that triggers every day starting at 12:01am that repeats every minute until 11:59pm then goes through the value template node to then trigger some sort of automation once it gets above 5 but… Honestly I’d recommend we have you build a sensor by extracting the attribute of the current power, then we can use that newly created sensor to trigger an automation for you inside Node-Red. Here’s how I would do it (note: the format might vary depending on how you have your sensors.yaml if you even have one of those, how I’m typing this up is assuming you do the sensor just how I have it written below inside your configuration.yaml). If you do have a sensors.yaml and perform the “!include” feature let me know we can adjust as needed.

sensor:
  - platform: template 
    sensors:
	  washing_machine_power:
        value_template: "{{ states.switch.washing_machine_.attributes.current_power_w | int }}"
	    friendly_name: 'Washing Machine Power'

Try setting this up and reboot, you should have a new sensor called “sensor.washing_machine_power” that will show the same number as the attribute from the switch (in your screen shot it’s showing 0.175 right now). Once you have that we can use the new sensor inside Node-Red to create your automation. If you’d rather go the other way with everything inside Node-Red like I mentioned at the beginning let me know, we’d just need to figure out a way to trigger something to hit that attribute repeatedly until it passes 5 and triggers something.

3 Likes

Thanks!! I think I’ll create that sensor extracted from the attribute.

This might be a little too late, but here is what I came up with. I haven’t had a chance to run it through its paces yet, but testing the state change in HA make it send the alert.

Basically I have a HA node watching the state change of the washer, then it pipes to a HA Trigger:state with some conditions like you mentioned earlier, then it sends to the alert.

In case you are still wondering how to do it without an additional sensor:

Simply use a switch node and enter the desired event.data like this:
Capture

My example is for the Xiaomi Magic Cube.

You can check any payload by using a debug node and selecting complete msg object in its output option. Then you can checkout the full message and select the path or value easily by using this menu:

Below you can find the code for the example above including the debug node.

[{“id”:“ca8138d.a992ac8”,“type”:“server-state-changed”,“z”:“34604278.54647e”,“name”:“Magic Cube”,“server”:“2fba4297.e4145e”,“entityidfilter”:“binary_sensor.cube_158d00024815e0”,“entityidfiltertype”:“substring”,“outputinitially”:false,“state_type”:“str”,“haltifstate”:“”,“halt_if_type”:“”,“halt_if_compare”:“is”,“outputs”:1,“x”:70,“y”:1020,“wires”:[[“7a76cb20.7b5044”,“31bea645.794eca”]]},{“id”:“7a76cb20.7b5044”,“type”:“switch”,“z”:“34604278.54647e”,“name”:“Flip90?”,“property”:“data.new_state.attributes.last_action”,“propertyType”:“msg”,“rules”:[{“t”:“eq”,“v”:“flip90”,“vt”:“str”}],“checkall”:“true”,“repair”:false,“outputs”:1,“x”:240,“y”:1020,“wires”:[[“c79ad474.bbe128”,“31bea645.794eca”]]},{“id”:“c79ad474.bbe128”,“type”:“api-call-service”,“z”:“34604278.54647e”,“name”:“”,“server”:“2fba4297.e4145e”,“service_domain”:“media_player”,“service”:“media_play_pause”,“data”:“{"entity_id":"media_player.uberall"}”,“render_data”:false,“mergecontext”:“”,“output_location”:“payload”,“output_location_type”:“msg”,“x”:490,“y”:1020,“wires”:[]},{“id”:“31bea645.794eca”,“type”:“debug”,“z”:“34604278.54647e”,“name”:“”,“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“true”,“x”:360,“y”:1100,“wires”:},{“id”:“2fba4297.e4145e”,“type”:“server”,“z”:“”,“name”:“Home Assistant”,“legacy”:false,“hassio”:true,“rejectUnauthorizedCerts”:true,“ha_boolean”:“y|yes|true|on|home|open”}]

2 Likes

Reviving a very old thread, because it helped me dig into a solution.

If anyone is still looking for how to do this without any extra nodes, or sensors, I dug through the source for node-red and figured out how to do it. I have an automation to lower my receiver volume when I switch to my PS4 because otherwise it can be very loud.

You can compare against a property:




Which initially I thought pulled directly from the entity, but it does not, it pulls from the constraintTarget.state (which you can see in the failed comparator output if you use a debug node):



So I was able to trigger on new and old states with this:


Works like a charm now!
4 Likes

Well thanks for digging it up, because your post pointed me right to the brightness values on lights, which allowed me to enforce minimum brightness while on for certain picky LED fixtures.

I was also looking for a similar solution, that I was able to do like so.
Can anyone help me with replacing the circled part with one Function nod?

[{"id":"ffb08fd6.0b4dc","type":"server-state-changed","z":"45465411.cc0a6c","name":"Climate is heating?","server":"72b04927.1f5688","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"climate.thermostat","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"x":110,"y":340,"wires":[["2eb22370.c8995c"]]},{"id":"8f460835.65f018","type":"api-call-service","z":"45465411.cc0a6c","name":"","server":"72b04927.1f5688","version":1,"debugenabled":false,"service_domain":"fan","service":"turn_on","entityId":"fan.second_bedroom_fan","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":640,"y":280,"wires":[[]]},{"id":"7c4e3419.f4c90c","type":"api-call-service","z":"45465411.cc0a6c","name":"","server":"72b04927.1f5688","version":1,"debugenabled":false,"service_domain":"fan","service":"turn_off","entityId":"fan.second_bedroom_fan","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":820,"y":400,"wires":[[]]},{"id":"2eb22370.c8995c","type":"switch","z":"45465411.cc0a6c","name":"","property":"data.new_state.attributes.hvac_action","propertyType":"msg","rules":[{"t":"eq","v":"heating","vt":"str"},{"t":"eq","v":"idle","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":340,"wires":[["c9c1c605.1f3b58","229eac34.017734"],["4b4c4d59.346b24"]]},{"id":"c9c1c605.1f3b58","type":"switch","z":"45465411.cc0a6c","name":"","property":"data.new_state.attributes.hvac_action","propertyType":"msg","rules":[{"t":"neq","v":"data.old_state.attributes.hvac_action","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":280,"wires":[["8f460835.65f018"]]},{"id":"4b4c4d59.346b24","type":"switch","z":"45465411.cc0a6c","name":"","property":"data.new_state.attributes.hvac_action","propertyType":"msg","rules":[{"t":"neq","v":"data.old_state.attributes.hvac_action","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":400,"wires":[["a0cd550d.4744f8"]]},{"id":"a0cd550d.4744f8","type":"trigger","z":"45465411.cc0a6c","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"10","extend":false,"units":"min","reset":"stop","bytopic":"all","name":"","x":630,"y":400,"wires":[["7c4e3419.f4c90c"]],"icon":"node-red/timer.svg"},{"id":"229eac34.017734","type":"change","z":"45465411.cc0a6c","name":"stop timer","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":340,"wires":[["a0cd550d.4744f8"]]},{"id":"72b04927.1f5688","type":"server","z":"","name":"Home Assistant","addon":true}]

Unfortunately your post seems to have become dated. I do not see the Constraints section anywhere. Did you find a new solution? Please share!

1 Like

Here is how I do it with a homekit connected ecobee and the hvac_action attribute.

[{"id":"858850f8.d94488","type":"tab","label":"ECOBEE","disabled":false,"info":""},{"id":"5b5b9e4378990062","type":"group","z":"858850f8.d94488","name":"HVAC - Return Air Booster On/Off","style":{"label":true,"stroke":"#000000","fill":"#e3f3d3","color":"#000000"},"nodes":["6f81d9184d2ba8ea","290a93436704232b","6aa89b12dde1fb54","d96505bee79ed87d","9afa4e5e4c6260b7","de16bbc0282104c6","21cea1d0f4205453","27cc7b22e27e5b4f","08432ba0f098e0d5"],"x":34,"y":39,"w":1052,"h":222},{"id":"6f81d9184d2ba8ea","type":"inject","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"Run Manually","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":220,"wires":[["290a93436704232b"]]},{"id":"290a93436704232b","type":"api-current-state","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"Ecobee Thermostat","server":"39dd3b2a.d5a744","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"climate.thermostat_temp_ecobee_2","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":270,"y":140,"wires":[["9afa4e5e4c6260b7","de16bbc0282104c6"]]},{"id":"6aa89b12dde1fb54","type":"switch","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"heating","vt":"str"},{"t":"eq","v":"cooling","vt":"str"},{"t":"eq","v":"idle","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":730,"y":140,"wires":[["d96505bee79ed87d","27cc7b22e27e5b4f"],["d96505bee79ed87d","27cc7b22e27e5b4f"],["d96505bee79ed87d","08432ba0f098e0d5"]],"outputLabels":["heating","cooling","idle"]},{"id":"d96505bee79ed87d","type":"debug","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"hvac state?","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":80,"wires":[]},{"id":"9afa4e5e4c6260b7","type":"change","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"hvac_action --> payload","rules":[{"t":"move","p":"data.attributes.hvac_action","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":140,"wires":[["6aa89b12dde1fb54"]]},{"id":"de16bbc0282104c6","type":"debug","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"ecobee attributes","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"data","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":80,"wires":[]},{"id":"21cea1d0f4205453","type":"server-state-changed","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"Ecobee Thermostat State","server":"39dd3b2a.d5a744","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"climate.thermostat_temp_ecobee_2","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"}],"x":170,"y":80,"wires":[["290a93436704232b"]]},{"id":"27cc7b22e27e5b4f","type":"api-call-service","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"ON - Return Booster","server":"39dd3b2a.d5a744","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":["43128e40993b123d0d8546b1302ccc5d"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":960,"y":140,"wires":[[]]},{"id":"08432ba0f098e0d5","type":"api-call-service","z":"858850f8.d94488","g":"5b5b9e4378990062","name":"OFF - Return Booster","server":"39dd3b2a.d5a744","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":["43128e40993b123d0d8546b1302ccc5d"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":960,"y":200,"wires":[[]]},{"id":"39dd3b2a.d5a744","type":"server","name":"Home Assistant","addon":true}]

The trigger state node was added to the palette after this post was created. You can use conditions inside the node to trigger off an attribute. Using the old state attribute as well as the new state will limit the node to only fire only when the state changes. Attribute properties will auto fill when you start typing.

1 Like

This works brilliantly, although I had to list the hvac_action modes as an “in” condition because I want it to trigger on heating, cooling, and idle. I then told the trigger to output each of those separately.

Also, is there a better way to stop the trigger from sending the same payload over and over again besides using a filter node? Right now I’m limiting the payload to value changes only through my “report-value-change” filter node. But it seems silly to separate, combine, and then separate the hvac_action modes to stop a repeating payload issue.

1 Like

You need the comparison against the old state. prev entity is the same as old_state. Use that for the entity condition. It will need to pass that test before moving to the custom output conditions.