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