Using attribute as trigger for automation

Hi,

I’m trying to write an automation to trigger when i change the set value of the thermostat. I’m using the generic_thermostat ( climate.aneks.room1 ).

I have tried with the following , but it never triggers.

 - alias: 'Set thermostat room1'
    trigger:
       platform: template
       value_template: "{{ state_attr('climate.aneks_room1','temperature')  }}"
    action:
      - service: notify.pushbullet
        data:
          message: 'Thermostat is changed'

Any thoughts on how this can be solved ?

2 Likes

It’s not indented correctly. Should be:

 - alias: 'Set thermostat room1'
   trigger:
     platform: template
     value_template: "{{ state_attr('climate.aneks_room1','temperature')  }}"
   action:
     - service: notify.pushbullet
       data:
         message: 'Thermostat is changed'

Thanks,

I think that is just the copy and paste devil that changed my code when posted here :-s. The code that I have in my automation.yaml is indented correctly. Still it doesn’t fire when the set point of the thermostat changes.

1 Like

Check the developer tools states menu. Do you have the correct entity_id and attribute (they are case sensitive).

The template trigger should evaluate to ‘true’ to trigger.

You can make a template sensor from your climate attribute and trigger the automation with the state trigger from that sensor.

2 Likes

Add a test to the value_template:

value_template: "{{ state_attr('climate.aneks_room1','temperature') | int > 0 }}"

When the temperature changes, the template will evaluate to true and execute the action (assuming the temperature is always above zero).

I think you are on to something.
Tried this , but it only triggers the first time I change the set temperature, it doesn’t trigger on consecutive changes.

if you’d create a template sensor for the attribute temperature, you can use that sensor as trigger: state for the automation.

You’re right. I just tested it and the temperature has to fall below 0 first (to cause the template to evaluate to false) and only then will it will trigger on the next temperature above 0. I was hoping it didn’t work that way, but it does.


EDIT
Basically, it works the same way as above and below in numeric_state trigger. In this example the temperature has to ‘cross the threshold’ in order cause a trigger. In other words, if the temperature is already above the threshold of zero, further changes above the threshold won’t cause triggering.

1 Like

One way to do it would be to break the attribute out using a template sensor then use a state trigger.

EDIT: nope that wont work either. Needs to be numeric state platform for a sensor.

Maybe a trend binary sensor?

- platform: trend
  sensors:
    room1_thermostat_changed:
      entity_id: sensor.thermostat_room1_setting
      attribute: temperature

Automation

- alias: 'Set thermostat room1'
    trigger:
       platform: state
       entity_id: binary_sensor.room1_thermostat_changed
       to: 'on'
    action:
     - service: notify.pushbullet
       data_template:
         message: 'Room1 thermostat has been changed to {{ state_attr('climate.aneks_room1','temperature') }}'

EDIT2: I think that will only work for raising the thermostat (default min_gradient = 0). You’d need a sensor for decreasing as well.

- platform: trend
  sensors:
    room1_thermostat_changed_down:
      entity_id: sensor.thermostat_room1_setting
      attribute: temperature
      invert: true

Solved it by building a template sensor for the attribute set temperature and used a state trigger on that sensor.

When I change the set temperature on the thermostat , the sensor changes and triggers the automation.

Dammit! That was my first reply but I edited it because I did not think this trigger would work:

    trigger:
       platform: state
       entity_id: sensor.thermostat_room1_setting

Thought you would have to use a numeric_state trigger.

why would you say that?
I have this related automation, based on the state changes of all my energy sensors, and works just fine. I’ve disabled it because it senses the change on all of my sensors, and it gets triggered too often… :wink: My example below is using the platform: event, event_type: state_changed.

Using trigger: state should result in the same effect?

creating a trigger state for only one sensor should just work?
has the OP tried it?

- alias: 'Activate Map sensors actueel'
  id: 'Activate Map sensors actueel'
  initial_state: 'off'
  trigger:
    platform: event
    event_type: state_changed
    event_data:
      domain: sensor
  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id.endswith('actueel')}}
  action:
    service: python_script.map_sensors_actueel

only thing one could add is a condition for the state to change more than 1 degree, or half a degree, and not trigger on other attributes of the entity_id (trigger: state also triggers on attribute changes, so have to filter these out if not desired.)

Because I’m an idiot. I’ve been making nothing but mistakes today.

1 Like

VDRainer had provided this solution:

In fairness, it should be VDRainer’s post that is marked as the ‘Solution’.

2 Likes

Hey, all. You could do this with just an automation. E.g.:

  - alias: 'Set thermostat room1'
    trigger:
      platform: state
      entity_id: climate.aneks_room1
    condition:
      condition: template
      value_template: >
        {{ trigger.from_state and
           trigger.to_state.attributes.temperature !=
           trigger.from_state.attributes.temperature }}
    action:
      service: notify.pushbullet
      data:
        message: Thermostat is changed
11 Likes

Thanks, it works fine!
I wasn’t sure, because trigger is on the thermostat state which remains “heat” during a temperature setting change, so I would expect no trigger - but it works, indeed…
Could you explain why? State trigger platform perhaps includes all attributes also, not only the main state of an entity?

I believe that’s correct - ANY change of ANY attribute will trigger the state automation even if its state string remains the same. That’s why state objects have last_changed and last_updated attributes.

Actually, it’s literally there:

If only entity_id is given trigger will activate for all state changes, even if only state attributes change.

2 Likes

I can’t wrap my head around why you have to use “trigger.from_state” in the template. Why?
Just want to learn the logic.

      value_template: >
        {{ trigger.from_state and
           trigger.to_state.attributes.temperature !=
           trigger.from_state.attributes.temperature }}

That’s verifying that from_state exists because at startup the automation will fire but won’t have a from_state

1 Like