Automation - Passing variable into conditions

Hi,
I’m new into Home assistant - automation. I dig through many materials, but it seems I still can’t find the answer to a simple question…

How to put variable instead of a static number in automation conditions.

This gives me an error:
Message malformed: expected float for dictionary value @ data['below']

Do you have any good and up to date tutorials about automation scripts in Home assistant?


The whole script in automation.yaml looks like that:

- id: '1590229314352'
  alias: Nowa automatyzacja
  description: ''
  trigger:
  - minutes: /1
    platform: time_pattern
  condition:
  - below: states('input_number.temperatura_parter')
    condition: device
    device_id: c291ead262d8422b81ff9c576f04f010
    domain: sensor
    entity_id: sensor.0x00158d00042fdc94_temperature
    type: is_temperature
  action:
  - device_id: 84edb4fc5b78498b83a57a8b6ce41123
    domain: switch
    entity_id: switch.0x04cf8cdf3c76497a_switch
    type: turn_on

Replace your condition with this:

  condition:
  - condition: template
    value_template: "{{ states('sensor.0x00158d00042fdc94_temperature')|float < states('input_number.temperatura_parter')|float }}"

There is a document on templating here:

2 Likes
  - condition: template
2 Likes

@tom_l , @123 ,
Not certain why (probably because all the documentation says so) but I always thought that conditions in automations were different (nested) to scripts (single lined)
Is this just a stupid convention (mine) that I’ve been following ?

Thank you. Edited.

Interesting so this is a template…
I was wondering about this one, but for me the name was kind of misleading.
Anyway, thanks! I have replaced Condition with your code and now it seems that it doesn’t want to start at all. Does anyone know the reason?

- id: '1590245574925'
  alias: Włącz ogrzewanie
  description: ''
  trigger:
  - minutes: /5
    platform: time_pattern
  condition:
  - condition: template
    value_template: 'value_template: "{{ states(''sensor.0x00158d00042fdc94_temperature'')|float < states(''input_number.temperatura_parter'')|float }}"'
  action:
  - device_id: 84edb4fc5b78498b83a57a8b6ce41123
    domain: switch
    entity_id: switch.0x04cf8cdf3c76497a_switch
    type: turn_on

Did you do a configuration check before reloading automations or restarting?

What error did it give?

What errors are there in the developer tools logs?

 value_template: 'value_template: "{{ states(''sensor.0x00158d00042fdc94_temperature'')|float < states(''input_number.temperatura_parter'')|float }}"'

should be:

 value_template: "{{ states(''sensor.0x00158d00042fdc94_temperature'')|float < states(''input_number.temperatura_parter'')|float }}"
2 Likes

Good catch. That’s the second time I’ve seen someone do that and missed it. Must be something to do with people pasting into the UI editor.

Interesting. Configuration correct, reboot done, no errors.

PS. I have copied it as you have written so probably this second value_template: was added by automation itself when converting this to yaml.

Ok, but it was giving me some error.
I had to change it to:

    value_template: '{{ states(''sensor.0x00158d00042fdc94_temperature'')|float <       states(''input_number.temperatura_parter'')|float }}'

it seems that this '' before {{ and after }} was a problem

Anyway it seems it is running now. Many thanks!

Why are you triggering this automation every 5 minutes? Why not just trigger it when the temperature falls below the value in the input_number?

This 5 minute is only for testing purposes.

The setup is looking like that:
The biggest room has a temperature sensor (WSDCGQ11LM) it connects via CC2531 and zigbee2mqtt to socket plug ZNCZ04LM. This one is starting and stopping a 25kW gas boiler.

So right now to turn this on, the condition will be that every 30 minutes it will check if the measured temperature is lower then setpoint minus 0,5 degrees (negative part of hysteresis).

To turn off every 5 minutes we will check if the measured temperature is higher then setpoint plus 0,5 degrees (positive part of hysteresis)

Hopefully thanks to that I will avoid too often switching on and off this gas boiler.

Is this right or wrong we will see. If you have better ideas please share. I still can’t test it because I’m waiting for a new gas boiler.

I don’t see the need to “check temperature every 30 minutes”. Even real thermostats don’t work that way (i.e. polling). They’re event-based, namely when thresholds are crossed (i.e. temperature rises above or falls below setpoint values).

I suggest you consider using the Generic Thermostat integration. You can specify hysteresis using the hot_tolerance and cold_tolerance options. You can also specify minimum operating time with min_cycle_duration.

Yes, you are right.

About this Generic thermostat - I would love to use it but I have only does two elements that I have mentioned and neither of them is from Entity id of **climate** domain. Which seems to be a must to run this.

I have copied the example from the documentation and substituted your entities in the appropriate places:

climate:
  - platform: generic_thermostat
    name: Heater
    heater: switch.0x04cf8cdf3c76497a_switch
    target_sensor: sensor.0x00158d00042fdc94_temperature
    min_temp: 17
    max_temp: 24
    ac_mode: false
    target_temp: 21
    cold_tolerance: 0.5
    hot_tolerance: 0
    min_cycle_duration:
      seconds: 120
    initial_hvac_mode: "off"
    away_temp: 16
    precision: 0.1

You will need to adjust the values to suit your preferences.

I have set min_cycle_duration to 120 seconds. You need to read the instructions for you gas boiler to determine the correct value. Basically, it means when the Generic Thermostat enables the gas boiler, it will allow it to run for no less than 120 seconds (it may need to be higher). This is to prevent what is known as ‘short-cycling’. It typically takes time for a gas appliance to startup and begin producing heat. You don’t want the Generic Thermostat to enable heating and then (potentially) disable it just 30 seconds later. In contrast, an electric heating device can easily support a short cycle time (i.e. 15 seconds or even less).

2 Likes

Many thanks, I didn’t know that you can define this entity.
It looks like there are big possibilities in Home Assistant but sometimes documentation from the main source could be better. It looks like it is written for people that already know Home assistant and this programming language but it is not easy for people that are starting.

Again, thank you.

2 Likes

Taras’s post above is pretty much exactly what I use.
I leave the heating on 24/365 but the target temperature gets updated by time, occupancy and doors/windows open etc.

1 Like

Yes, I will have to write some scripts / automation or whatever that is called here, to adjust this Generic thermostat setpoint to time schedule (like night and day) and other but aslo leave some room for the user to do some temporary adjustments.