Trigger.entiry_id in conditions & a float problem

Hi there. Trying to create a single automation for something that seems to me actually quite straightforward - but apparently isn’t.

Below my automation. Gunnar is my robotic lawnmower. I wanted to see Gunnar’s status in GUI. Black if it’s idle in the dock, green when mowing, yellow when actually charging batteries. That’s simply done with floorplan, input_number.gunnar_lataa: 0: idle, 1: charging, 2: mowing. The Power (3.3W) is very strict, it’s 3.2-3.3W when mowing, and 3.3W-3.4W when idle. So I made the automation first without conditions, works quite nicely (Alhtough problem 2 occurs). However, that heiman smart plug reports its state like every 5 seconds, filling my logs with thousands of lines “sensor.heiman_smart_plug_1_power triggered automation gunnar_tila”… Then I thought of conditions: what if I trigger action only in case power reports matter - when crossing 3.3W or 4.0W. But I cannot get the conditions work with trigger.entity_id (error message “Problem 1” below the code). Any help here?

Here I could easily replace the entity_id with sensor.heiman_smart_plug_1_power and it will work. However I have another identical automation where I can not do that. Hence decided to ask this with shorter piece of code.

Anyway. Even if deleting conditions I have a float problem (error message “Problem 2” below). I tried to 1) remove the {% else %} -statement from the end (as it might be useless), then 2) changed all action return values from e.g. 1 to 1.0, and finally 3) changed action return values from 1 to “1.0 | float” (without quotes). No effect. Still the automation seems to work.

- id: Gunnar_tila
  alias: Gunnarin tila
  mode: queued
  trigger:
    platform: state
    entity_id: sensor.heiman_smart_plug_1_power
  condition:
    condition: or
    conditions:
      - condition: numeric_state
        entity_id: '{{ trigger.entity_id }}'
        below: 3.3
      - condition: numeric_state
        entity_id: '{{ trigger.entity_id }}'
        above: 3.3
      - condition: numeric_state
        entity_id: '{{ trigger.entity_id }}'
        below: 4.0
      - condition: numeric_state
        entity_id: '{{ trigger.entity_id }}'
        above: 4.0
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.gunnar_lataa
      value: >
        {% if states('sensor.heiman_smart_plug_1_power') | float < 3.3 %} 
          2
        {% elif states('sensor.heiman_smart_plug_1_power') | float > 4.0 %} 
          1 
        {% elif states('sensor.heiman_smart_plug_1_power') | float < 4.0 %}
          {% if states('sensor.heiman_smart_plug_1_power') | float > 3.3 %} 
            0 
          {% endif %}
        {% else %} 
          {{ states('input_number.gunnar_lataa') | float }}
        {% endif %}

Problem 1:

Invalid config for [automation]: Entity ID {{ trigger.entity_id }} is an invalid entity ID for dictionary value @ data['condition'][0]['conditions'][0]['entity_id']. Got None. (See ?, line ?). 

Problem 2:

Error while executing automation automation.gunnarin_tila: expected float for dictionary value @ data['value']
9:25:01 PM – (ERROR) Automation - message first occurred at 9:25:01 PM and shows up 2 times
Gunnarin tila: Error executing script. Invalid data for call_service at pos 1: expected float for dictionary value @ data['value']
9:25:01 PM – (ERROR) Automation - message first occurred at 9:25:01 PM and shows up 2 times

You can’t template that field. Good rule of thumb: if the field is outside service call or service data, it can’t be templated unless the docs mention it.

Damn. But thanks!

Why are you attempting to use trigger.entity_id in the condition when there’s only only one entity in the single State Trigger?

Will there be other entities eventually or is this automation meant to be generic so it can be used in a blueprint?

In the other automation I’m changing the colour of rooms (in floorplan) based on the temp. There using trigger.entity_id in conditions would have solved the problem of too many updates = too many automation triggers, filling logs and even creating errors (even mode: queued/parallel doesn’t always help)

And, thank you for the answers. I stopped troubleshooting something that was actually not supported, and started to look for other solutions. Just as simple as creating 3 triggers to the same automation:

  1. above 0, below 3.3
  2. above 3.3, below 4.0
  3. above 4.0, below 10000

At least according to documentation, having both below and above triggers only once when value enters the range. It’ll be triggered again only if the value has left the defined range and re-enters it.

After patient 5 min observation on the room temperatures (and sensor in the fridge) this seems to work great! Lawnmower will stop after couple of hours, but I’m confident. As always.

Those three triggers are the same as a single trigger using “above 0, below 10000” (unless you want to exclude the two threshold values of 3.3 and 4.0).

The only reason for using three separate triggers is if the logic in the automation’s action needs to know which one triggered the automation. In that case, it can determine the source via trigger.id.

Interesting information. Thanks. I haven’t been able to follow too well today the outcome, but the current status seems to be up to date - with WAY less automations triggered. Apparently still bit too many (as the lawnmower is 3.2-3.3 when mowing and 3.3-3.4 when docked, not charging) → so it is every now and then 3.3, which is outside the trigger range → and when coming back, it triggers the automation. Only few times a minute any more.

Charging might no’t work if the power consumption hops from 3.4W to e.g. 100W directly - both are in the range, probably no automation. Need to check. Wouldn’t really be a problem, I can find good enough values that would never be consecutive in power reports. This issue then becomes more academic.

Anyway, makes me wonder if I can get what I want (without trying to figure least probable consecutive values) with the suggested trigger_id. Basically change the action if/else -statements to trigger_id’s. Need to try.

You might want to consider using a Template Trigger instead of a State Trigger.

The advantage is that you can specify the value must be within a specific range for a minimum amount of time (like 3 seconds) before it will trigger the automation. That means minor, brief fluctuations (like from 3.1 to 3.2 and back to 3.1) won’t trigger the automation.

For example, this requires the sensor’s value to be greater than 0 and less than 3.3 for a minimum of 3 seconds in order for it to trigger.

  trigger:
    - platform: template
      value_template: "{{ 0 < states('sensor.heiman_smart_plug_1_power') | float < 3.3 }}"
      for: '00:00:03'

Very nice! That solves the fluctuation problem completely! Thanks a lot!

I tried to also use trigger id in the actions, but didn’t succeed. Simply giving each of the 3 triggers an id, and make 3 actions based on which one triggered. Not really an issue here, as the original data_template works fine (except that the last two if’s could be simplified to just %else% as below). For my curiosity, how would I convert the actions to use trigger id?

- id: Gunnar_tila
  alias: Gunnarin tila
  mode: queued
  trigger:
    - platform: template
      id: 'mowing'
      value_template: "{{ states('sensor.heiman_smart_plug_1_power') | float < 3.3 }}"
      for: '00:00:10'
    - platform: template
      id: 'docked'
      value_template: "{{ 3.3 < states('sensor.heiman_smart_plug_1_power') | float < 4.0 }}"
      for: '00:00:10'
    - platform: template
      id: 'charging'
      value_template: "{{ 4.0 < states('sensor.heiman_smart_plug_1_power') | float }}"
      for: '00:00:10'
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.gunnar_lataa
      value: >
        {% if states('sensor.heiman_smart_plug_1_power') | float < 3.3 %} 
          2
        {% elif states('sensor.heiman_smart_plug_1_power') | float > 4.0 %} 
          1 
        {% else %} 
          0
        {% endif %}

Only documentation I could find was 2021.7: A new entity, trigger IDs and script debugging - Home Assistant and I just don’t get it…

Here’s one way to use trigger.id in the action.

- id: Gunnar_tila
  alias: Gunnarin tila
  mode: queued
  trigger:
    - platform: template
      id: 'mowing'
      value_template: "{{ states('sensor.heiman_smart_plug_1_power') | float < 3.3 }}"
      for: '00:00:10'
    - platform: template
      id: 'docked'
      value_template: "{{ 3.3 < states('sensor.heiman_smart_plug_1_power') | float < 4.0 }}"
      for: '00:00:10'
    - platform: template
      id: 'charging'
      value_template: "{{ 4.0 < states('sensor.heiman_smart_plug_1_power') | float }}"
      for: '00:00:10'
  action:
    service: input_number.set_value
    target:
      entity_id: input_number.gunnar_lataa
    data:
      value: '{{ {"mowing": 2, "docked": 1, "charging": 0}[trigger.id] }}'