Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95414

Help with templates for conditions and actions

$
0
0

@nealedowling wrote:

Here is a more concise version of my request. I have left the original below for clarity and reference.

I need help in writing a template.
The template should perform a simple subtractions of the value of one entity’s state from the value of another entity’s state. The answer will always be one of: -4, -3, -2, -1, 0, 1, 2, 3 or 4.

If the result is 0 - nothing should be done. I can use this as a condition I guess.
If it is -2 or -3, it should execute action 1
If it is -1 or 4, it should execute action 2
If it is -4 or 1, it should execute action 3
It it is -3 or 2, it should execute action 4.

I really cannot see where the errors are, segments in the Template tool in developer tools shows that each part is evaluating OK.

Check Config gives this:

Configuration invalid

Invalid config for [automation]: invalid template (TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘elif’ or ‘else’ or ‘endif’. The innermost block that needs to be closed is ‘if’.) for dictionary value @ data[‘action’][0][‘data_template’][‘entity_id’]. Got None. (See /config/configuration.yaml, line 180). Please check the docs at https://home-assistant.io/integrations/automation/

so it wants what I have provided? Hmm - I’m sure the issue is me, but ?

As I have split my automations into separate yaml files, the line numbers aren’t of any use.
I do know that the below code is the culprit, as when it is commented out, the config check passes.

Any help appreciated.

# When the flame is set in front end, check current flame level and set accordingly.
#If it is -2 or -3, it should execute script.stove_flame_minus_two
#If it is -1 or 4, it should execute script.stove_flame_minus_one
#If it is -4 or 1, it should execute script.stove_flame_plus_one
#It it is -3 or 2, it should execute script.stove_flame_plus_two
- alias: Align stove flame level to stone flame set after 10 seconds
  trigger:
    platform: template
    value_template: "{{ (as_timestamp(states.sensor.time.last_changed)|int) - (as_timestamp(states.input_select.stove_flame_set.last_changed)|int) > 10 }}"
  condition:
    condition: template
    value_template: "{{ (states('input_select.stove_flame_set')|float) == (states('sensor.stove_flame_level')|float) }}"
  action:
    service: script.turn_on
    data_template:
      entity_id: >-
        {% if (states('sensor.stove_flame_level')|float) - (states('input_select.stove_flame_set')|float) == -2 %}
          script.stove_flame_minus_two
        {% elif (states('sensor.stove_flame_level')|float) - (states('input_select.stove_flame_set')|float) == -3 %}
          script.stove_flame_minus_two
        {% elif (states('sensor.stove_flame_level')|float) - (states('input_select.stove_flame_set')|float) == -1 %}
          script.stove_flame_minus_one
        {% elif (states('sensor.stove_flame_level')|float) - (states('input_select.stove_flame_set')|float) == 4 %}
          script.stove_flame_minus_one
        {% if (states('sensor.stove_flame_level')|float) - (states('input_select.stove_flame_set')|float) == -4 %}
          script.stove_flame_plus_one
        {% elif (states('sensor.stove_flame_level')|float) - (states('input_select.stove_flame_set')|float) == 1 %}
          script.stove_flame_plus_one
        {% elif (states('sensor.stove_flame_level')|float) - (states('input_select.stove_flame_set')|float) == -3 %}
          script.stove_flame_plus_two
        {% elif (states('sensor.stove_flame_level')|float) - (states('input_select.stove_flame_set')|float) == 2 %}
          script.stove_flame_plus_two
        {% else %}
          script.najd
        {% endif %}

Hello.

looking for some help to see things a bit more clearly. I’ve been trying to work out an efficient way to do this without creating 20 or so automations. Here goes with the explanation:

I have a stove that I am able to control with HA.
I can control: On, Off, Flame level.

I’m good with on and off automations.

The flame level control is a bit of a hack as it has 5 levels, but can only be controlled by a remote using the + or - buttons. There is no return information to check that the flame set level is the desired level, so going blind on it.

I have scripts set up in HA to make Flame +1, Flame +2, Flame-1 and Flame-2.
If I need to go from Flame 1 to Flame 5 - Flame-1 does the job. (Its cyclical, so when I press + on 5 I get to 1 and when I press - on 1 I get to 5). These work reliably with my Broadlink RM3 mini through HA.
This way with +1, +1, -1 and -2 I can get from any position to any other.

Here is an example of the scripts. Please ignore the delays, they are related to multiple presses on an IR remote:

stove_flame_minus_one:
  alias: 'Send Flame - command twice'
  sequence:
  - service: switch.turn_on
    entity_id: switch.pellet_stove_flamedown
  - delay:
      milliseconds: 600
  - service: switch.turn_on
    entity_id: switch.pellet_stove_flamedown

stove_flame_minus_two:
  alias: 'Send Flame - command thrice'
  sequence:
  - service: switch.turn_on
    entity_id: switch.pellet_stove_flamedown
  - delay:
      milliseconds: 600
  - service: switch.turn_on
    entity_id: switch.pellet_stove_flamedown
  - delay:
      milliseconds: 400
  - service: switch.turn_on
    entity_id: switch.pellet_stove_flamedown

I need to be able to set the flame level from the frontend (Lovelace or HADashboard). As there are 5 levels, I have set up an input_select entity (input_select.stove_flame_desired) to select the desired flame level. This may be achieved by pressing a + or - button on HADashboard to get to the desired level, so I am incorporating delays prior to doing anything.

I have also set up a mqtt sensor “Stove Flame Level” to retain the level once any automation changes it.

I would like to create one automation (if possible) that:

  • Is triggered 10 seconds after the state of “input_select.stove_flame_desired” changes.
  • Calculates the difference between “Stove Flame Level” and “input_select.stove_flame_desired” ((input_select.stove_flame_desired) - (Stove Flame Level))
  • calls the appropriate script (see following table for values and actions:

Now, I can create multiple automations, as I said, but there must be a better way of achieving this.

Here is the start of my automation. Any ideas please?

(See above)

My ultimate goal is to leave the stove turned on where possible still staying within the defined temperature ranges. I will achieve this by change the flame setting based on based on desired temperature, maximum temperature and current temperature.
eg. The stove starts on a winter morning where the temperature sensor is at 16 degrees, and the desired temperature is 22.6, the maximum at 23.8
Set the flame to level 5 until it reaches 1.5 degree below the desired temperature.
I am providing the additional information so that once I get it set up, if anyone is interested, I will post the code. Also, I know there are some very clever people in this forum that will challenge my logic and show me another way of doing it that may be more effective!

Many thanks

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 95414

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>