Struggling to write more than basic automations (in visual editor / YAML)

Have made a basic heating controller that adjusts morning temperature based on the weather but its getting unwieldy.

But I want to do something like this (pseudo code):

trigger 07:00:00 # heating on
  if(sunny)
    temperature_var = 17
  else
    temperature_var = 19

trigger 18:00:00 # heating evening
  temperature_var = 20

trigger 22:00:00 # heating off
  temperature_var = 9

#Common code
set thermostat = temperature_var # set the thermostat to desired temperature

I am used to using if then else and variables to help with logic (eg in C, PHP etc).

Can YAML do this ?

Thanks Chris

Yes. How are you detecting sunniness, and how are you adjusting your thermostat?

There are multiple ways to accomplish your goal. If your goal is to accomplish it using only the UI editor you should look into the Choose Action.

Alternatively, you can use Trigger variables and a bit of Templating. What follows is an outline using those tools, based on some assumptions about your entities.

trigger:
  - platform: time
    at: "07:00:00"
    variables:
      temperature_var: >
        {{ iif( is_state('sensor.YOUR_WEATHER_SENSOR', 'sunny'), 17, 19) }}
  - platform: time
    at: "18:00:00"
    variables:
      temperature_var: 20
  - platform: time
    at: "22:00:00"
    variables:
      temperature_var: 9
condition: []
action:
  - service: climate.set_temperature
    data:
      temperature:  "{{temperature_var}}"
    target:
      entity_id: climate.YOUR_THERMOSTAT

EDIT: Corrected typo noted below. Thanks Troon!

1 Like

Typo there: entity_id.

1 Like

Oh great, I can stop tearing my hair out :partying_face:

I’m detecting sunny like this:

              - condition: or
                conditions:
                  - condition: state
                    entity_id: weather.forecast_home
                    state: clear-night
                  - condition: state
                    entity_id: weather.forecast_home
                    state: partlycloudy
                  - condition: state
                    entity_id: weather.forecast_home
                    state: sunny

I set the thermostat like this:

        sequence:
          - service: climate.set_temperature
            data:
              temperature: 19
            target:
              device_id: 5d1294cd9e728403e50fdae353865273

Oh I thought variables were local in scope, but that looks great.

Hopefully I can figure out the then/else bit.

Thank you.

Automations can have variables of script level scope as well as ones that are local to a given block.

You can put all your acceptable “sunny” values into the variable definition template:

trigger:
  - platform: time
    at: "07:00:00"
    variables:
      temperature_var: >
        {% set sunny = is_state( 'weather.forecast_home', 
        ['clear-night', 'partlycloudy', 'sunny'] )  %}
        {{ iif( sunny, 17, 19 ) }}
  - platform: time
    at: "18:00:00"
    variables:
      temperature_var: 20
  - platform: time
    at: "22:00:00"
    variables:
      temperature_var: 9
condition: []
action:
  - service: climate.set_temperature
    data:
      temperature:  "{{ temperature_var }}"
    target:
      device_id: 5d1294cd9e728403e50fdae353865273

Thank you very much for your help.
I am refining the script and will post later in case anyone is interested,

For anyone who may be interested below is my latest YAML for heating.

Background
Before HA I set the heating to come on to 17C at 07:00
The problem was that if it was a sunny morning the heating was wasted and also created unwanted noise.

So now at 07:00 I check the local weather and if it is not cloudy I set the heating to 0.5C below the current room temperature.

If it is not sunny I set the temperature to room temperature but limited to 17-19C

alias: Heating
description: ""
trigger:
  - platform: time
    at: "07:00:00"
    variables:
      temperature_var: >
        {% set targettemp = states('sensor.living_room_temperature') | float %} 
        {% if is_state('weather.forecast_home', ['clear-night', 'partlycloudy', 'sunny'] )  %}
          {% set targettemp = targettemp - 0.5 %} 
        {% else %}
          {% set targettemp = targettemp + 1 %} 
          {% set targettemp = iif(targettemp < 17, 17, targettemp) %} 
        {% endif %} {% set targettemp = iif(targettemp > 19, 19, targettemp) %} 
        {{ targettemp }}
  - platform: time
    at: "16:00:00"
    variables:
      temperature_var: 21
  - platform: time
    at: "19:00:00"
    variables:
      temperature_var: 19
  - platform: time
    at: "22:00:00"
    variables:
      temperature_var: 9
  - platform: time
    at: "23:30:00"
    variables:
      temperature_var: 9
condition: []
action:
  - service: climate.set_temperature
    data:
      temperature: "{{ temperature_var | float }}"
    target:
      device_id: 5d1294cd9e728403e50fdae353865273
mode: single

Future enhancements could be to look to see if there will be any sun in the morning not just first thing.