Please help with automation

Hi. I need to run this automation or script or just anything :slight_smile: Someone says this can´t be automation because any conditions will be bypassed if it is started by another automation. I have an automation now which should START this:
automation

Thanks

This is that logic as an automation

trigger:
- platform: state
  entity_id: sensor.temp_sensor
action:
- service_template: >
    {% set t = trigger.to_state.state | float %}
    {% set state_a = is_state('switch.a', 'on') %}
    {% set state_b = is_state('switch.b', 'on') %}
    {% if t > 50 %}
      switch.turn_{{ 'off' if state_b else 'on' }}
    {% else %}
      switch.turn_{{ 'off' if state_a else 'on' }}
    {% endif %}
  data_template:
    entity_id:
      {% set t = trigger.to_state.state | float %}
      {% set state_a = is_state('switch.a', 'on') %}
      {% set state_b = is_state('switch.b', 'on') %}
      {% if t > 50 %}
        switch.{{ 'b' if state_b else 'a' }}
      {% else %}
        switch.{{ 'a' if state_a else 'b' }}
      {% endif %}
3 Likes

Oh, thank you so much @petro! Do you think it will be problem if I will “fire” your automation by another automation? In fact I´m not sure if it is possible…

@petro Are you sure you have both cases under 50°C and over 50°C in your automation? There is {% if t > 50 %} two times. Maybe should be one {% if t > 50 %} and second {% if t < 50 %} ?

No, if else statements capture above and below 50. The logic is correct because if it’s not above 50 it’s below. If … else… get it?

As for the other automation firing this, i’m not sure what you mean. You can strip out the action section and use it as a script but you’d have to remove the trigger.to_state.state and replace it with the entity that you’d be getting the info from.

Now I have my own automation which checks switches (room floor heating valves) and if any of those switches is in ON state it should run your automation which will start heating.

Here is my automatin. Last part - action should run your automation:

- id: '1577880069664'
  alias: Zapni topeni
  description: ''
  trigger:
  - minutes: '/5'
    platform: time_pattern
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: switch.koupelna
        state: 'on'
      - condition: state
        entity_id: switch.mala_loznice
        state: 'on'
      - condition: state
        entity_id: switch.detsky_pokoj
        state: 'on'
      - condition: state
        entity_id: switch.obyvak_topeni
        state: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.cerpadlo_aku_podlahovka

Ok, then convert it to a script and add it at the script service at the bottom. No point in having the other automation.

So my scripts.yaml will be:

action:
- service_template: >
    {% set t = trigger.to_state.state | float %}
    {% set state_a = is_state('switch.a', 'on') %}
    {% set state_b = is_state('switch.b', 'on') %}
    {% if t > 50 %}
      switch.turn_{{ 'off' if state_b else 'on' }}
    {% else %}
      switch.turn_{{ 'off' if state_a else 'on' }}
    {% endif %}
  data_template:
    entity_id:
      {% set t = trigger.to_state.state | float %}
      {% set state_a = is_state('switch.a', 'on') %}
      {% set state_b = is_state('switch.b', 'on') %}
      {% if t > 50 %}
        switch.{{ 'b' if state_b else 'a' }}
      {% else %}
        switch.{{ 'a' if state_a else 'b' }}
      {% endif %}

regarding to my automation - I should use - service: script.petro_script and save your script as …(how)? Script should have some alias or should be saved as separated file?

Well script as I wrote it into scripts.yaml has some problem:
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/home/homeassistant/.homeassistant/scripts.yaml”, line 13, column 8
(btw. I didn´t rename my switches yet - hope this is not the problem…)

post your scripts.yaml portion that has the code from above.

but two things right off the top…

you don’t include the “action:” line in a script. if you have that in there remove it. it should be “sequence:”

then the next thing (and likely your problem in the error) is that you are missing a “multi-line” operator after “entity_id:”

data_template:
  entity_id: >  #<----here
    {% set t = trigger.to_state.state | float %}
    {% set state_a = is_state('switch.a', 'on') %}
    {% set state_b = is_state('switch.b', 'on') %}
    {% if t > 50 %}
      switch.{{ 'b' if state_b else 'a' }}
    {% else %}
      switch.{{ 'a' if state_a else 'b' }}
    {% endif %}

My actual scripts.yaml:

action:
- service_template: >
    {% set t = trigger.to_state.state | float %}
    {% set state_a = is_state('switch.a', 'on') %}
    {% set state_b = is_state('switch.b', 'on') %}
    {% if t > 50 %}
      switch.turn_{{ 'off' if state_b else 'on' }}
    {% else %}
      switch.turn_{{ 'off' if state_a else 'on' }}
    {% endif %}
  data_template:
    entity_id:
      {% set t = trigger.to_state.state | float %}
      {% set state_a = is_state('switch.a', 'on') %}
      {% set state_b = is_state('switch.b', 'on') %}
      {% if t > 50 %}
        switch.{{ 'b' if state_b else 'a' }}
      {% else %}
        switch.{{ 'a' if state_a else 'b' }}
      {% endif %}

Then see my first “off the top” suggestion above.

and then once you figure out the correct syntax for a script then see my second recommendation to add the multi-line operator.

Now I´m lost - my scripts.yaml:

sequence:
  - service_template: >
      {% set t = trigger.to_state.state | float %}
      {% set state_a = is_state('switch.a', 'on') %}
      {% set state_b = is_state('switch.b', 'on') %}
      {% if t > 50 %}
        switch.turn_{{ 'off' if state_b else 'on' }}
      {% else %}
        switch.turn_{{ 'off' if state_a else 'on' }}
      {% endif %}
    data_template:
      entity_id: sensor.aku_teplota
        {% set t = trigger.to_state.state | float %}
        {% set state_a = is_state('switch.a', 'on') %}
        {% set state_b = is_state('switch.b', 'on') %}
        {% if t > 50 %}
          switch.{{ 'b' if state_b else 'a' }}
        {% else %}
          switch.{{ 'a' if state_a else 'b' }}
        {% endif %}

Shows error:

Invalid config for [script]: expected a dictionary for dictionary value @ data[‘script’][‘sequence’]. Got [OrderedDict([(‘service_template’, “{% set t = trigger.to_state.state | float %} {% set state_a = is_state(‘switch.a’, ‘on’) %} {% set state_b = is_state(‘switch.b’, ‘on’) %} {% if t > 50 %}\n switch.turn_{{ ‘off’ if state_b else ‘on’ }}\n{% else %}\n switch.turn_{{ ‘off’ if state_a else ‘on’ }}\n{% endif %}\n”), (‘data_template’, OrderedDict([(‘entity_id’, "sensor.aku_teplota {% set t = trigger.to_state.state | float %} {% set state_a = is_state(‘switch.a’, ‘on’) %} {% set state_b = is_sta… (See /home/homeassistant/.homeassistant/configuration.yaml, line 67). Please check the docs at https://home-assistant.io/integrations/script/

Look at the documentation… what are you missing? Study it. You need to learn this yourself. Compare the top lines of your script.yaml file to the script example.

Sorry but didn´t find anything about multi line operator… YAML was validated so don´t know what could be wrong. I´m not a programmer and the true is those examples are sometimes useless (some of them even not working in latest versions of HASS). Better is to find someone´s github files. OK - I go to search for something. Thanks.

You don’t need to be a programmer for this to work. The information needed to create a script is just wrong. It’s clear that you don’t understand the configuration portion of this and you need to learn it. This is the example in the documents:

  message_temperature:
    sequence:
      # This is Home Assistant Script Syntax
      ...

This is yours:

sequence:
  ...

What are you missing?

OK, done, now I have valid scripts.yaml - correct?

  topeni:
    alias: 'Spousteni topeni'
    sequence:
      - service_template: >
          {% set t = trigger.to_state.state | float %}
          {% set state_a = is_state('switch.a', 'on') %}
          {% set state_b = is_state('switch.b', 'on') %}
          {% if t > 50 %}
            switch.turn_{{ 'off' if state_b else 'on' }}
          {% else %}
            switch.turn_{{ 'off' if state_a else 'on' }}
          {% endif %}
        data_template:
          entity_id: sensor.aku_teplota
            {% set t = trigger.to_state.state | float %}
            {% set state_a = is_state('switch.a', 'on') %}
            {% set state_b = is_state('switch.b', 'on') %}
            {% if t > 50 %}
              switch.{{ 'b' if state_b else 'a' }}
            {% else %}
              switch.{{ 'a' if state_a else 'b' }}
            {% endif %}

Yet another question - after I will prepare those 2 switches (named differently than A and B) I should change only ‘switch.a’ and ‘switch.b’ nothing else? How about state_a and state_b ?

no look at the code i posted in my example above.

Look for the comment after “entity_id:” and write it exactly how I have it there. You need to remove the extraneous entity_id that you added (for some unknown reason) and add the “>” to it.