Problem with automation rules

I found earlier post from @Festyk, witch help me quit a lot steps to forward, but I can’t get my automation to work ;(
In My Configuration.yaml file I describe switch
switch:
- platform: mqtt
heater_switch:
state_topic: “stat/TestTH/POWER”
command_topic: “cmnd/TestTH/POWER”
payload_on: “ON”
payload_off: “OFF”
qos: 0
retain: true
friendly_name: Lyliti

And in automation.yaml the automation itself

- alias: Lylita radikat
  trigger:
    platform: state
    entity_id: input_number.target_temp
  action:
    service_template: >
      {% if states.sensor.testth_temp | float < states.input_number.target_temp | float %}
      switch.turn_on
      {% elif states.sensor.testth_temp | float  > states.input_number.target_temp | float %}
      switch.turn_off
      {% endif %}
    entity_id:
      - switch.mqtt_switch

switch from ui works but when I slide the temp. slider then I got error
2017-10-29 05:23:51 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: expected a dictionary for dictionary value @ data[‘action’][0][‘data_template’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 163). Please check the docs at https://home-assistant.io/components/automation/
2017-10-29 05:38:57 ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service:

where is my mistake ? the first error is telling that i must use data_template ? - where ?
the states are:
input_number.target_temp: 29.0
sensor.testth_temp: 23.4

Try wrapping some brackets around the expressions:

      {% if (states('sensor.testth_temp') | float) < (states('input_number.target_temp') | float) %}

I’d also drop the elif and just use else. Otherwise if they’re the same you get an error with no service.

1 Like

if I changed my code like

- alias: Lylita radikat
  trigger:
   platform: state
   entity_id: input_number.target_temp
  action:
   service_template: >
      {% if (states('sensor.testth_temp') | float < (states('input_number.target_temp') | float) %}
      switch.turn_on
      {% else (states('sensor.testth_temp') | float >= (states('input_number.target_temp') | float) %}
      switch.turn_off
   entity_id:
      - switch.mqtt_switch

check_config dont pass the check and gives error

That’s about as useful as telling a car garage the “the car doesn’t start”. You need to tell us what the error is.

That said, the error is pretty clear:

   service_template: >
      {% if (states('sensor.testth_temp') | float < (states('input_number.target_temp') | float) %}
      switch.turn_on
      {% else %}
      switch.turn_off
      {% endif %}

else has no options, and you must provide the closing statement. I would advise that you read the documentation.

error is
2017-10-29 15:11:58 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘}’, expected ‘)’) for dictionary value @ data[‘action’][0][‘service_template’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 163). Please check the docs at https://home-assistant.io/components/automation/

   service_template: >-
      {% if (states('sensor.testth_temp') | float) < (states('input_number.target_temp') | float) %}
      switch.turn_on
      {% else %}
      switch.turn_off
      {% endif %}

Try that one, in fixing everything else it looks like a bracket got lost

thanks @Tinkerer, your lines was perfect for my this problem, can you help me add another trigger to my automation?
Because now it’s seems works with the slider but when temperature itself changes then sonoff don’t switch OFF when the temperature is higher witch I gave with slider or when temp going to below to given value

and can I give some default value to sensor ? - it’s because when I to some changes and restart the service, then max 5 minute when sonoff sending the sensor information and the automation gives errors until that. The value is “-” until first session to HA from sonoff sensor.

Just add a second trigger:

trigger:
  - platform: state
    entity_id: sensor.fish
    to: 'on'
  - platform: state
    entity_id: switch.furnace
    to: 'off'
      {% if (states('sensor.testth_temp')|default(0)|float) < (states('input_number.target_temp')|float) %}

That’ll default sensor.testth_temp to zero if it doesn’t provide a value.

If you’re looking to turn the device on or off depending on the temperature, it might make more sense to have a template sensor that reflects the state you want. Then you could have an automation (or automations) that turns the switch on if the sensor is on, and off when it’s off. That’ll then handle when the temperature starts above, or below, your target.

hi thanks for advice to automation and trigger - thous works fine, but default value seems don’t works
I got error until the first value from testth is coming
2017-10-29 18:17:35 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 241, in _step
result = coro.throw(exc)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/discovery.py”, line 127, in scan_devices
results = yield from hass.async_add_job(_discover, netdisco)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/discovery.py”, line 153, in _discover
netdisco.scan()
File “/srv/homeassistant/lib/python3.5/site-packages/netdisco/discovery.py”, line 57, in scan
self.ssdp.scan()
File “/srv/homeassistant/lib/python3.5/site-packages/netdisco/ssdp.py”, line 40, in scan
self.update()
File “/srv/homeassistant/lib/python3.5/site-packages/netdisco/ssdp.py”, line 88, in update
entry for entry in scan()
File “/srv/homeassistant/lib/python3.5/site-packages/netdisco/ssdp.py”, line 257, in scan
response = sock.recv(1024).decode(“utf-8”)
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xc9 in position 2: invalid continuation byte

Once again big thanks to you @Tinkerer
I but my working automation for electric radiator example here as well:

- alias: radiator automation
  trigger:
  - platform: state
    entity_id: input_number.target_temp
  - platform: state
    entity_id: sensor.testth_temp
  action:
   service_template: >-
    {% if (states('sensor.testth_temp') | float) > (states('input_number.target_temp') | float) %}
    switch.turn_off
    {% else %}
    switch.turn_on
    {% endif %}
   entity_id:
      - switch.mqtt_switch