Set variable in automation BEFORE the condition section

In the script below, I’d like to se the variable villkor-sekunder in the beginning of the automation, before the conditions-section. This is because the value of that variable is used in the conditions later in the script.

How is this possible?

I just want my script to run on certain times and by declaring that variable, it will be easy to decide when to run the script. I know i might use a classic time-condition, but that would only clutter the code.

  - alias: "Media: Starta radion i köket vid rörelse"
    trigger: 
      platform: state
      entity_id: binary_sensor.multisensor_koksfonstret_sensor
      to: 'on'
    condition:
      - condition: state 
        entity_id: timer.kitchen_radio_delay
        state: 'active'
      - condition: state
        entity_id: media_player.koket
        state: 'off'
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ now().weekday() in [ 0,1,2,3,4 ] and villkor-sekunder > 20400 and villkor-sekunder < 27000 }}"
          - condition: template
            value_template: "{{ now().weekday() == 5 and villkor-sekunder > 28800 and villkor-sekunder < 36000 }}"
          - condition: template
            value_template: "{{ now().weekday() == 6 and villkor-sekunder > 28800 and villkor-sekunder < 36000 }}"
    action:
      - service: input_select.select_option
        data_template:
          entity_id: input_select.chromecast_audio_kitchen
          option: >
            {% set sekunder = now().hour * 3600 + now().minute* 60 + now().second %}
            {% if now().weekday() in [ 0,1,2,3,4 ] and sekunder > 20400 and sekunder < 27000 %}
              Sveriges Radio P1
            {% elif now().weekday() == 5 and sekunder > 28800 and sekunder < 36000 %}
               Sveriges Radio P4 Stockholm
            {% elif now().weekday() == 6 and sekunder > 28800 and sekunder < 36000 %}
              Sveriges Radio P1 
            {% endif %}

Move the conditions in to the action block, set the value, run the condition check then preform the final action.

Note that the automation entity itself will now trigger every time the binary sensor switches on and the variable set, but the original action will only be carried out if the conditions are true.

- alias: "Media: Starta radion i köket vid rörelse"
  trigger:
    platform: state
    entity_id: binary_sensor.multisensor_koksfonstret_sensor
    to: 'on'
  action:
    - service: YOUR_VARIABLE_SET_SERVICE_HERE
      data: YOUR_VALUE
    - condition: state
      entity_id: timer.kitchen_radio_delay
      state: 'active'
    - condition: state
      entity_id: media_player.koket
      state: 'off'
    - condition: or
      conditions:
        - condition: template
          value_template: "{{ now().weekday() in [ 0,1,2,3,4 ] and villkor-sekunder > 20400 and villkor-sekunder < 27000 }}"
        - condition: template
          value_template: "{{ now().weekday() == 5 and villkor-sekunder > 28800 and villkor-sekunder < 36000 }}"
        - condition: template
          value_template: "{{ now().weekday() == 6 and villkor-sekunder > 28800 and villkor-sekunder < 36000 }}"
    - service: input_select.select_option
      data_template:
        entity_id: input_select.chromecast_audio_kitchen
        option: >
          {% set sekunder = now().hour * 3600 + now().minute* 60 + now().second %}
          {% if now().weekday() in [ 0,1,2,3,4 ] and sekunder > 20400 and sekunder < 27000 %}
            Sveriges Radio P1
          {% elif now().weekday() == 5 and sekunder > 28800 and sekunder < 36000 %}
            Sveriges Radio P4 Stockholm
          {% elif now().weekday() == 6 and sekunder > 28800 and sekunder < 36000 %}
            Sveriges Radio P1 
          {% endif %}
2 Likes

Thanks alot, @anon43302295.
I had no idea one could put conditions in the action-section of an automation.

I’m not quite sure about this line:

service: YOUR_VARIABLE_SET_SERVICE_HERE
data: YOUR_VALUE

What kind of VARIABLE_SET_SERVICE are there to chose from?

In my updated code, I just changed the second line VALUE to

service: YOUR_VARIABLE_SET_SERVICE_HERE
data_template: '{% set sekunder = now().hour * 3600 + now().minute* 60 + now().second %}'

How do I alter that first line?

I don’t know, depends on how you’re storing the variable. Input_number? Variable custom component?

If input_number it would be eg:

- service: input_number.set_value
  data:
    entity_id: input_number.villkor_sekkunder
    value: 3600
- condition: state 
  [ ... ]
1 Like