Automation trigger.state always FALSE?

Hi,

I am confused. I created an automation which actions depend on the state of the trigger:

alias: Helper E_Hzg Status
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.r_ehzg
    to: null
conditions: []
actions:
  - action: >-
      input_boolean.turn_{% if(trigger.state == '1') %}on{% else %}off{% endif
      %}
    target:
      entity_id: input_boolean.helper_ehzg
    data: {}
mode: single

The automation fires whenever the state of sensor.r_ehzgchanges. But the result of the template is always input_boolean.turn_off even if the state of the trigger is “1” (should be input_boolean.turn_on in this case).

The r_ehzg is a mqtt sensor and when checking on the sensor history it displays ‘1’ or ‘0’ so I expected this to work.

What am I doing wrong here?

Thanks!

/KNEBB

Use:

trigger.to_state.state

See here:

Fixed, and slightly simplified:

- action: input_boolean.turn_{{ 'on' if trigger.to_state.state == '1' else 'off' }}
1 Like

You’ll also want to remove this or change it to to: ~ depending on what you want.

Why? From the state trigger docs:

To trigger on all state changes, but not on changed attributes, set at least one of from, to, not_from, or not_to to null.

What does to: ~ do, and where is that documented?

triggers:
  # This will trigger on any state or attribute change.
- trigger: state
  entity_id: ...

  # This will trigger on any state change, and will ignore attributes
- trigger: state
  entity_id: ...
  to: ~

It’s not documented. And maybe that None thing is new.

For large values of new. Time flies when you’re having fun:

The code change (as opposed to the documentation) uses ~ in the description:

1 Like

@all:
I want to trigger on ANY state change (0 to 1 as well as 1 to 0).
Please keep it simple- I want it really based on "KISS" and not by any fancy or undocumented features!

@Troon
Not working. Changed to this as you suggested:

action: input_boolean.turn_{{ 'on' if trigger.to_state.state == '1' else 'off' }}
target:
  entity_id: input_boolean.dummy_helper_status_ehzg
data: {}

Results in an error when the automation is executed:
Error rendering service name template: UndefinedError: 'dict object' has no attribute 'to_state'

You can’t manually test automations that use trigger. Wait for, or force a new state in your sensor.

See here:

The Trigger ID or any data passed by in the trigger data in conditions or actions can’t be tested directly this way.

1 Like

Well well well now, we are dealing with a YAMLism. TIL apparently ~ is another way to write null, or None in yaml.

2 Likes

@Troon :expressionless: Ok, understood. Forced my sensor now to see the reaction of the trigger. Still the same. Always “off”.

I downloaded the trace but I do not see anything I can make use of. Shall I post the trace here? But it has a couple of lines

Some might be useful:

 "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "sensor.r_ehzg",
              "from_state": {
                "entity_id": "sensor.r_ehzg",
                "state": "0",
                "attributes": {
                  "friendly_name": "r_ehzg"
                },
                "last_changed": "2024-10-15T13:33:15.671384+00:00",
                "last_reported": "2024-10-15T13:33:15.671384+00:00",
                "last_updated": "2024-10-15T13:33:15.671384+00:00",
                "context": {
                  "id": "01JA86410Q6APJ9C9E74YKTFRX",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "sensor.r_ehzg",
                "state": "1",
                "attributes": {
                  "friendly_name": "r_ehzg"
                },
                "last_changed": "2024-10-15T13:33:53.665938+00:00",
                "last_reported": "2024-10-15T13:33:53.665938+00:00",
                "last_updated": "2024-10-15T13:33:53.665938+00:00",
                "context": {
                  "id": "01JA8656415WWWDHVHYQ2V7HG5",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of sensor.r_ehzg"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-10-15T13:33:53.681658+00:00",
          "changed_variables": {
            "context": {
              "id": "01JA86564G6BWQAMMRYY70S2GW",
              "parent_id": "01JA8656415WWWDHVHYQ2V7HG5",
              "user_id": null
            }
          },
          "result": {
            "params": {
              "domain": "input_boolean",
              "service": "turn_off",
              "service_data": {},
              "target": {
                "entity_id": [
                  "input_boolean.dummy_helper_status_ehzg"
                ]
              }
            },
            "running_script": false
          }
        }
      ]
    },
    "config": {
      "id": "1728457392271",
      "alias": "Helper E_Hzg Status",
      "description": "",
      "triggers": [
        {
          "trigger": "state",
          "entity_id": [
            "sensor.r_ehzg"
          ],
          "to": null
        }
      ],
      "conditions": [],
      "actions": [
        {
          "action": "input_boolean.turn_{% if(trigger.state == '1') %}on{% else %}off{% endif%}",
          "target": {
            "entity_id": "input_boolean.dummy_helper_status_ehzg"
          },
          "data": {}
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01JA86564G6BWQAMMRYY70S2GW",
      "parent_id": "01JA8656415WWWDHVHYQ2V7HG5",
      "user_id": null
    }
  },

Any further ideas?

/KNEBB

You didn’t change the template


Or you’re looking at the wrong trace.

2 Likes

No, I did not change because when I change to the syntax which was suggested I got an error (see above).

Knebb, I pulled the information directly from your trace. You either didn’t update it or you used the wrong trace.

If you updated it, your trace should have if(trigger.to_state.state == '1'...

1 Like

@knebb To be clear, you need to both:

  • update the template back to the one I suggested; and
  • test it by forcing or waiting for a sensor update

Hi,

your example somehow did not work, but simply inserting the .to_statedid the trick!

Here is it, now working:

alias: Helper E_Hzg Status
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.r_ehzg
    to: null
conditions: []
actions:
  - action: input_boolean.turn_{{ 'on' if trigger.to_state.state == '1' else 'off' }}
    target:
      entity_id: input_boolean.dummy_helper_status_ehzg
    data: {}
mode: single

Thanks a lot, you guys rock!

/KNEBB


and what you pasted is, character-perfect, my example.

:smiley: I do not licke to be picky
 yes, the first line in your first post did the trick. But initially I used you second example like this:

- action: input_boolean.turn_{{ 'on' if trigger.to_state.state == '1' else 'off' }}

And this did not work, as written above.

But really, really thank you- in the end it was the right thing and I am happy it is working now.

/KNEBB

Yes, but that didn’t work because you clicked the test button, not because the template was bad.

1 Like