Using trigger.entity_id in automation condition

I have an automation that monitors sever servers and takes action if they are offline. I’m trying to a add a condition to the automation that uses trigger.entity_id but I’m having difficulty.

Here is the automation. It works as designed without the condition:

alias: Mining status monitor 
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.miner01
      - sensor.miner02
      - sensor.miner03
      - sensor.miner04
      - sensor.miner05
      - sensor.miner06
      - sensor.miner07
      - sensor.miner08
      - sensor.miner09
      - sensor.miner10
    for:
      hours: 0
      minutes: 5
      seconds: 0
    to: 'false'
    attribute: miner_online
condition:
  - condition: numeric_state
    entity_id: {{ trigger.entity_id }}
    attribute: miner_hashrate
    below: '1'
action:
  - service: notify.notify
    data:
      message: >
        Miner {{ trigger.entity_id.split('.')[-1] }} has been offline in HiveOS
        for 5 minutes.  Power cycling now.
      title: Miner offline in HiveOS
  - service: switch.turn_off
    target:
      entity_id: switch.{{ trigger.entity_id.split('.')[-1] }}
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: switch.turn_on
    target:
      entity_id: switch.{{ trigger.entity_id.split('.')[-1] }}
mode: single

I get the following error when I try to add the condition:

Message malformed: Entity [object Object] is neither a valid entity ID nor a valid UUID for dictionary value @ data['condition'][0]['entity_id']

Any assistance is appreciated!

There are limits on where templates can be used, I believe that is one of them. Just use a template condition instead.

alias: Mining status monitor 
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.miner01
      - sensor.miner02
      - sensor.miner03
      - sensor.miner04
      - sensor.miner05
      - sensor.miner06
      - sensor.miner07
      - sensor.miner08
      - sensor.miner09
      - sensor.miner10
    for:
      hours: 0
      minutes: 5
      seconds: 0
    to: 'false'
    attribute: miner_online
condition:
  - condition:  "{{ state_attr(trigger.entity_id, 'miner_hashrate') < 1 }}"
action:
  - service: notify.notify
    data:
      message: >
        Miner {{ trigger.entity_id.split('.')[-1] }} has been offline in HiveOS
        for 5 minutes.  Power cycling now.
      title: Miner offline in HiveOS
  - service: switch.turn_off
    target:
      entity_id: switch.{{ trigger.entity_id.split('.')[-1] }}
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: switch.turn_on
    target:
      entity_id: switch.{{ trigger.entity_id.split('.')[-1] }}
mode: single

Tip:

Instead of this:

{{ trigger.entity_id.split('.')[-1] }}

you can use this:

{{ trigger.to_state.object_id }}

I tried that, but get the following error:

Message malformed: Unexpected value for condition: ‘{{ state_attr(trigger.entity_id, ‘miner_hashrate’) < 1 }}’. Expected and, device, not, numeric_state, or, state, sun, template, time, trigger, zone @ data[‘condition’][0]

I ended up using:

condition:
  - condition: template
    value_template: "{{ state_attr('trigger.entity_id', 'miner_hashrate') int(0) < 1 }}"

Which worked.

Thanks for the tip!

I found this while looking for a similar need but it is apparently not exactly the same solution. I need to set the value of a variable when either of 2 conditions are set (or statement). I currently have the varThe variable value getting set correctly but when the switch is on but when it is turned off, the value goes away. Is there a way of locking in the value by using the trigger event or another method to keep the variable value set even when the switch is turned off? Here is what I am using now to update the variable

- platform: template
  sensors:
    hvac_setpoint_preferred:
      friendly_name: "Preferred Occupied Temp Setpoint"
      entity_id:
        - sensor.hvac_setpoint
      value_template: >-
          {% set indoorTempsetpoint_preferred = states('sensor.hvac_setpoint')| float %}
          {% if is_state('switch.den_lamp_3', 'on') or is_state('switch.dresser', 'on') %}
          {{ (indoorTempsetpoint_preferred) }}
          {% endif %}
      unit_of_measurement: "F"

Not related to this topic. Try this, with a template that returns a value in either situation:

- platform: template
  sensors:
    hvac_setpoint_preferred:
      friendly_name: "Preferred Occupied Temp Setpoint"
      value_template: >-
          {% set indoorTempsetpoint_preferred = states('sensor.hvac_setpoint')|float(0) %}
          {% if is_state('switch.den_lamp_3', 'on') or is_state('switch.dresser', 'on') %}
            {{ (indoorTempsetpoint_preferred) }}
          {% else %}
            {{ this.state }}
          {% endif %}
      unit_of_measurement: "°F"

I’ve changed your unit_of_measurement from Farads to degrees Fahrenheit and removed your entity_id line, which hasn’t been needed for ages. Did ChatGPT come up with this?

Thank you. No, I can’t blame ChatGPT, it’s all on me! As I’ve been learning to utilize the power and features of Home Assistant, some of the examples I’ve found to help guide me are old. I didn’t realize until recently that the template format had been updated so I’ll begin learning to adapt old solutions to new template formats.

Thanks again.

1 Like