Trigger only if changed from value is not undefined

I currently experience troubles with automation triggers that fire even though there was no information from the entity.
For instance:


The small gaps in between are outages of the service that delivers the information - so there is no information at all at this time.

My trigger currently reacts to change from “no request” to any other state. (there are a lot of possible states the service returns, therefore I’d like to keep it that way) Now I would like to add an extra condition to only trigger the automation if there is a value in the first place and if that value is not equal to “no request”.

How can I achieve this?

It is easier to give an answer specific to your situation if you share your automation.

If the trigger is already limited to firing on state changes from no request, then your false triggers should only be happening when the sensor enters an outage. If you aren’t already using a to, you can use the not_to configuration variable to avoid those triggers.

trigger:
  - platform: state
    enitity_id: sensor.status_heat_pump
    from: 'no request'
    not_to:
      - unknown
      - unavailable

If you are already using a to variable to limit triggering to certain states, you will need to use a condition.

condition:
  - condition: template
    value_template: "{{ trigger.from_state.state not in ['unknown','unavailable'] }}"
1 Like

This is how the automation is currently defined:

- id: '1670399918475'
  alias: Heizungsverteiler einschalten
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.status_heat_pump
    from: No request
    for:
      hours: 0
      minutes: 1
      seconds: 0
  condition:
  - condition: not
    conditions:
    - condition: or
      conditions:
      - condition: state
        entity_id: sensor.status_heat_pump
        state: unavailable
      - condition: state
        entity_id: sensor.status_heat_pump
        state: unknown
  action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.heizungsverteiler
  mode: restart

I’ll try your suggestion with not_to

Even with not_to set the automation still triggers if there is no value from the sensor.
What’s the state’s name if there is no data at all?

Download the trace json from one of these false trigger runs then copy/paste it here so we can see what’s going on.

Here’s the trace of the last run.

{
  "trace": {
    "last_step": "action/0",
    "run_id": "04b13c870e4f445e118c1bbf82cb7f2d",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2023-09-08T13:53:11.518907+00:00",
      "finish": "2023-09-08T13:53:11.649875+00:00"
    },
    "domain": "automation",
    "item_id": "1670399918475",
    "trigger": "state of sensor.status_heat_pump",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2023-09-08T13:53:11.518961+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.heizungsverteiler_einschalten",
              "state": "on",
              "attributes": {
                "id": "1670399918475",
                "last_triggered": "2023-09-07T03:48:11.157215+00:00",
                "mode": "restart",
                "current": 0,
                "friendly_name": "Heizungsverteiler einschalten"
              },
              "last_changed": "2023-09-05T16:56:51.604180+00:00",
              "last_updated": "2023-09-07T03:48:11.190751+00:00",
              "context": {
                "id": "01H9PW2B8M17FDP8N6Z79H51PW",
                "parent_id": "01H9PW0GNC6ZK9CQVM8HJXBW7B",
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "sensor.status_heat_pump",
              "from_state": {
                "entity_id": "sensor.status_heat_pump",
                "state": "No request",
                "attributes": {
                  "friendly_name": "status_heat_pump"
                },
                "last_changed": "2023-09-07T03:52:07.575271+00:00",
                "last_updated": "2023-09-07T03:52:07.575271+00:00",
                "context": {
                  "id": "01H9PW9J4P4V8QQYV20PF0D7JT",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "sensor.status_heat_pump",
                "state": "",
                "attributes": {
                  "friendly_name": "status_heat_pump"
                },
                "last_changed": "2023-09-08T13:52:11.511803+00:00",
                "last_updated": "2023-09-08T13:52:11.511803+00:00",
                "context": {
                  "id": "01H9TH117JG0SV79K01K61GF78",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": {
                "__type": "<class 'datetime.timedelta'>",
                "total_seconds": 60
              },
              "attribute": null,
              "description": "state of sensor.status_heat_pump"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2023-09-08T13:53:11.519409+00:00",
          "changed_variables": {
            "context": {
              "id": "01H9TH2VTYMMXNZAZ5XPS6BAXQ",
              "parent_id": "01H9TH117JG0SV79K01K61GF78",
              "user_id": null
            }
          },
          "result": {
            "params": {
              "domain": "switch",
              "service": "turn_on",
              "service_data": {},
              "target": {
                "entity_id": [
                  "switch.heizungsverteiler"
                ]
              }
            },
            "running_script": false
          }
        }
      ]
    },
    "config": {
      "id": "1670399918475",
      "alias": "Heizungsverteiler einschalten",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "sensor.status_heat_pump"
          ],
          "from": "No request",
          "not_to": [
            "unknown",
            "unavailable"
          ],
          "for": {
            "hours": 0,
            "minutes": 1,
            "seconds": 0
          }
        }
      ],
      "action": [
        {
          "service": "switch.turn_on",
          "data": {},
          "target": {
            "entity_id": "switch.heizungsverteiler"
          }
        }
      ],
      "mode": "restart"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01H9TH2VTYMMXNZAZ5XPS6BAXQ",
      "parent_id": "01H9TH117JG0SV79K01K61GF78",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Heizungsverteiler einschalten",
      "message": "triggered by state of sensor.status_heat_pump",
      "source": "state of sensor.status_heat_pump",
      "entity_id": "automation.heizungsverteiler_einschalten",
      "context_id": "01H9TH2VTYMMXNZAZ5XPS6BAXQ",
      "when": 1694181191.519132,
      "domain": "automation"
    },
    {
      "when": 1694181191.649389,
      "state": "on",
      "entity_id": "switch.heizungsverteiler",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Heizungsverteiler einschalten",
      "context_message": "triggered by state of sensor.status_heat_pump",
      "context_source": "state of sensor.status_heat_pump",
      "context_entity_id": "automation.heizungsverteiler_einschalten"
    }
  ]
}

The sensor’s state changed from No request to an empty string.

Add a State Condition, or Template Condition, to reject empty string values.

- id: '1670399918475'
  alias: Heizungsverteiler einschalten
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.status_heat_pump
    from: No request
    for:
      minutes: 1
  condition:
  - condition: template
    value_template: >
      {{ trigger.to_state.state not in ['', 'unknown', 'unavailable'] }}
  action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.heizungsverteiler
  mode: restart

Alternately, you can do what Didgeridrew suggested and use the State Trigger’s not_to option.

- id: '1670399918475'
  alias: Heizungsverteiler einschalten
  description: ''
  trigger:
  - platform: state
    enitity_id: sensor.status_heat_pump
    from: 'No request'
    not_to:
      - unknown
      - unavailable
      - ''
    for:
      minutes: 1
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.heizungsverteiler
  mode: restart