Node-RED trigger on attribute changes

I have a simple automation defined in HA to mange my fresh air vent when climate.hvac calls for heating. Here’s the code for that:

alias: "Ventilate during HVAC operation "
description: "Turn on outside vent for 5m when heat or cooling is activated. "
trigger:
  - platform: state
    entity_id:
      - climate.hvac
    attribute: hvac_action
    to: heating
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - platform: state
    entity_id:
      - climate.hvac
    attribute: hvac_action
    to: cooling
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition:
  - condition: numeric_state
    entity_id: sensor.back_yard_temperature
    above: 45
    below: 100
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id:
        - switch.relay_01_2
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id:
        - switch.relay_01_2
mode: single

This works, but there are some other decision points I want to add and I want this combined with my other HVAC nodes in Node-RED. However, pulling out the attributes is proving challenging. After some research I figured out how to trigger based on a change in attributes, but it also keeps re-deploying with other unrelated attribute changes (humidity and temp sensor for example). Here’s the view for my node that is working at the moment, but re-deploying unnecessarily:

All I want this to trigger on is when data.new_state.attributes.hvac_action is heating. I don’t know how to filter out changes in the other attributes.

2 Likes

Thank you! Been looking for a solution to this and this connected the dots for me. It’s working perfectly and makes perfect sense. I don’t know how I overlooked this before.

Thanks for the answer! I needed literally this a couple days ago.
I came up with a different solution but this one is definitely better.
I used the generic Event node, filtered for the state change event and desired entity and then later I checked in another node whether the hvac_action had changed.

The good thing with my approach is that with only one input node you can check for multiple properties changed (I am also interested when the preset changed for instance).
The bad thing is you receive many more events (e.g. every time the current temp changes).