How do I make automations for state changes?

I want to set a condition for state changed event attribute, so that automation triggers only when state switches on-press (including if it previously was in on-press too) and only if property counter is 1. how do I do that?

The bus receives:

[homeassistant.components.mqtt] Received message on zigbee2mqtt/0x0017880104xxxxxx: 
b'{"duration":0,"counter":1,"linkquality":28,"battery":100,"brightness":0,"action":"on-press"}'

[homeassistant.core] Bus:Handling <Event state_changed[L]: 
entity_id=sensor.dimmer, 
old_state=<state sensor.dimmer=unknown; duration=0, counter=1, linkquality=28, battery=100, brightness=0, action=on-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T14:45:20.005628+03:00>, 
new_state=<state sensor.dimmer=on-press; duration=0, counter=1, linkquality=28, battery=100, brightness=0, action=on-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T14:45:58.316584+03:00>>

Here’s what I’ve tried:

trigger:
  platform: state
  entity_id: sensor.dimmer
  to: on-press
condition:
  condition: template
  value_template: "{{ trigger.to_state.counter == 1 }}"

The condition doesn’t work. I’ve also tried new_state in template. How to write that properly?

it appears as if you don’t have your mqtt sensor set up properly. You have all the json attributes going to the state which keeps it a string. What does your configuration look like?

I’d expect it to look something like this:

sensor:
  - platform: mqtt
    state_topic: "home/sensor1/infojson"
    value_template: "{{ value_json.action }}"
    json_attributes_topic: "home/sensor1"

The state of the sensor would be the action, and the attributes would contain the counter among other things.

Then your automation would be:

trigger:
  platform: state
  entity_id: sensor.dimmer
  to: 'on-press'
condition:
  condition: template
  value_template: "{{ trigger.to_state.attributes.counter == 1 }

It was autoconfigured by zigbee2mqtt. Turns out I’ve missed .attributes, now it works

The only problem left is that state event doesn’t seem to fire if old state was the same, for example event (on-press, counter=1) -> (on-press, counter=2). Do I need to handle raw events to catch that?

ah ok, cool. Then you are good to go!

well… you could handle it with templates:

trigger:
  platform: state
  entity_id: sensor.dimmer
  to: 'on-press'
condition:
  condition: template
  value_template: >
    {% set oldcount = trigger.from_state.attributes.counter %}
    {% set newcount = trigger.to_state.attributes.counter %}
    {{ (newcount - oldcount) == 1 }}

That would see if the count changes by 1

I mean I don’t need to keep track of old count, just during my brief test it seems that if state was ‘on-press’ and become ‘on-press’ but with different attributes the event doesn’t seem to fire at all. Although I might have made a mistake during the testing.

Well that goes back to my original post. To me a state change should contain a state, not the entire attribute set. Can you take a screenshot of your sensor in the states page?

dimmer

Isn’t it always a state string plus a bunch of attributes? All my sensors seem to behave this way

Yah, the states page looks correct. I must be remembering wrong. I could have sworn the attributes were listed as ‘attributes’.

Either way, the trigger and conditions i posted should work fine. Both of them.

Ok, here’s some debug info

2019-05-02 15:43:51 [homeassistant.components.mqtt] Received message on zigbee2mqtt/0x0017880104xxxxxx: b'{"duration":0,"counter":1,"linkquality":47,"battery":100,"brightness":0,"action":"off-press"}'
2019-05-02 15:43:51 [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.dimmer, old_state=<state sensor.dimmer=off-press; duration=0, counter=1, linkquality=39, battery=100, brightness=0, action=off-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T15:43:47.669487+03:00>, new_state=<state sensor.dimmer=off-press; duration=0, counter=1, linkquality=47, battery=100, brightness=0, action=off-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T15:43:47.669487+03:00>>
2019-05-02 15:43:52 [homeassistant.components.mqtt] Received message on zigbee2mqtt/0x0017880104xxxxxx: b'{"duration":0,"counter":1,"linkquality":39,"battery":100,"brightness":0,"action":"off-press"}'
2019-05-02 15:43:52 [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.dimmer, old_state=<state sensor.dimmer=off-press; duration=0, counter=1, linkquality=47, battery=100, brightness=0, action=off-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T15:43:47.669487+03:00>, new_state=<state sensor.dimmer=off-press; duration=0, counter=1, linkquality=39, battery=100, brightness=0, action=off-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T15:43:47.669487+03:00>>
2019-05-02 15:43:54 [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.dimmer, old_state=<state sensor.dimmer=off-press; duration=0, counter=1, linkquality=39, battery=100, brightness=0, action=off-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T15:43:47.669487+03:00>, new_state=<state sensor.dimmer=unknown; duration=0, counter=1, linkquality=39, battery=100, brightness=0, action=off-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T15:43:54.003206+03:00>>
2019-05-02 15:43:54 [homeassistant.components.mqtt] Received message on zigbee2mqtt/0x0017880104xxxxxx: b'{"duration":0,"counter":1,"linkquality":39,"battery":100,"brightness":0,"action":"off-press"}'
2019-05-02 15:43:54 [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.dimmer, old_state=<state sensor.dimmer=unknown; duration=0, counter=1, linkquality=39, battery=100, brightness=0, action=off-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T15:43:54.003206+03:00>, new_state=<state sensor.dimmer=off-press; duration=0, counter=1, linkquality=39, battery=100, brightness=0, action=off-press, friendly_name=dimmer action, icon=mdi:gesture-double-tap @ 2019-05-02T15:43:54.205866+03:00>>
2019-05-02 15:43:54 [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Dimmer off, entity_id=automation.dimmer_off>

I’ve made the same automation for off button. I’ve been pressing off button repeatedly and you can see that at first events come through but the automation doesn’t trigger. However, after I’ve made a pause for 2 seconds it reverted to unknown state (as intended) and after that the press of off button triggered the automation, which leads me to believe that when source state equals new state, the state platform with to filter may ignore it.

it should just see it if you omit the from: field. You’re only using the to: field in your trigger so it should always trigger when to_state.state == ‘off-press’ regardless of what the from_state.state is equal to.

I don’t use from and it still doesn’t trigger