Using a variable to trigger an automation

Thanks to the suggestion of fetdot I successfully implemented the option to set the time & date for my alarm clock (see using a variable in an automation) using a Helper.

It also works to set a condition for the lightlevel Helper (input_number.livingroom_lightlevel) when the automation is trigger by a movement sensor.

It looks like this:

alias: Living Room - Light On
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.presence_9
    from: 'off'
    to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.lightlevel_12
    below: input_number.livingroom_lightlevel_12
action:
  - scene: scene.livingroom_light_on
mode: single

So far so good, but if I now want to switch off the lights, I get a message 'Message malformed: expected float for dictionary value @ data['above'] if I create a similar trigger that turns the light off when either nobody is in the room anymore or it becomes light enough at sunrise for example:

alias: Living Room - Light Off
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.presence_9
    from: 'on'
    to: 'off'
    for: '00:10:00'
  - platform: numeric_state
    entity_id: sensor.lightlevel_12
    above: input_number.livingroom_lightlevel | float
condition: []
action:
  - scene: scene.livingroom_light_off
mode: single

I also tried to convert it to a float,

    above: '{{ input_number.eetkamer_lichtniveau | float }}'

but here my knowledge of HA falls short. I do however have a strong feeling that there is some inconsistency here how things are implemented. At least I don’t see why it works for a Condition and not for a Trigger What would be the appropriate way to create this automation. It of course can be hard coded, but I prefer to allow my housemates to be able to set it at an appropriate light level.


My spider sense is tingling

Why do you need to convert it to a float in the light off automation? :thinking:
An input number already is a number, so you can just remove the float conversion

I first tried without the float conversion and in that case got the error message 'Message malformed: expected float for dictionary value @ data['above'] (forgot to mention that sorry)

The docs for numeric state triggers do not indicate that you can use an entity_id for the above/below keys.

You need a template trigger for this.

Marc seems to be right here :slight_smile:
The condition with a numeric_state supports templates, the trigger not :frowning:
Try this:

  - platform: template
    value_template: "{{ states('sensor.lightlevel_12') > 12 }}"
1 Like

:thinking:, but what should I do with the above: value? it keeps asking for that?

By the way, I suppose you meant this for the value template:

  - platform: numeric_state
    entity_id: sensor.lightlevel_12
    value_template: "{{ ( 'states.sensor.lightlevel_12' ) > ( 'states.input_number.livingroom_lightlevel' ) }}"
    above: 1000

No, don’t use a numeric_state trigger, use a template trigger. Yeah, missed the second entity.
Also try avoiding using state.domain.entity, instead use states('domain.entity'), this avoids errors when unavailable.
Complete automation should look like this:

alias: Living Room - Light Off
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.presence_9
    from: 'on'
    to: 'off'
    for: '00:10:00'
  - platform: template
    value_template: "{{ states('sensor.lightlevel_12') > states('input_number.livingroom_lightlevel') }}"
condition: []
action:
  - scene: scene.livingroom_light_off
mode: single
1 Like

Many thanks fetdot, that does the job!

For those who are running in the same issue

The entity_id should be:

    entity_id: sensor.lightlevel_12

but, that was just a minor slip of the pen. The original automation has two triggers. The latter one is now just switching off the lights in case it gets light again outside.

So the automation looks like:

alias: Living Room - Light Off
description: ''
trigger:
  - platform: state
    entity_id: sensor.lightlevel_12
    from: 'on'
    to: 'off'
    for: '00:10:00'
  - platform: template
    value_template: "{{ states('sensor.lightlevel_12') > states('input_number.livingroom_lightlevel') }}"
condition: []
action:
  - scene: scene.livingroom_light_off
mode: single