Race condition between sensor and slider?

Hi all,

At a wit’s end. I have this:

  - alias: Sync Set Thermo Value
    trigger:
      platform: state
      entity_id: sensor.desiredtemp
    action:
      - condition: and
        conditions:
          - condition: template
            value_template: '{{ states.sensor.desiredtemp.state != states.input_number.thermo_slider.state }}'
      - service: input_number.set_value
        data_template:
          entity_id: input_number.thermo_slider
          value: "{{ states.sensor.desiredtemp.state }}"

      
        
  - alias: Set Thermo Value from HASS
    trigger:
      platform: state
      entity_id: input_number.thermo_slider
    action:
      - service: shell_command.set_thermo_value
      - condition: and
        conditions:
          - condition: template
            value_template: '{{ states.input_number.thermo_slider.state != states.sensor.desiredtemp.state }}'

Something is wrong with Sync Thermo Value, because if I comment it out this doesn’t happen.

This is controlling yet another controllers virtual thermostat. Here I have two automations. One to set the foreign virtual thermostat to the sliders value. The other updates the slider based on the sensor value.

For some reason I can’t figure out, often times the result of changing the set temp either via HASS or the foreign controller, my desired temp gets set to a weird value of “-0.100000038” - and not just hass. It tries to set the foreign thermostat to that as well.

If I leave the “Sync Thermo Value” automation out - this doesn’t occur.

Any guidance?

If you have conditions in actions the list is executed in order. Actions progress down the list until a condition is not met then the automation exits. So the condition in your second automation does nothing as it is after the service call. It needs to be before the service call. Also you don’t need the and statement. Conditions are and by default and you only have one condition.

So, like this:

- alias: Set Thermo Value from HASS
    trigger:
      platform: state
      entity_id: input_number.thermo_slider
    action:
      - condition: template
        value_template: '{{ states.input_number.thermo_slider.state != states.sensor.desiredtemp.state }}'
      - service: shell_command.set_thermo_value

or like this as the only action is conditional for the whole automation:

- alias: Set Thermo Value from HASS
    trigger:
      platform: state
      entity_id: input_number.thermo_slider
    condition: template
      value_template: '{{ states.input_number.thermo_slider.state != states.sensor.desiredtemp.state }}'
    action:
      - service: shell_command.set_thermo_value

1 Like

thank you going to try this

Your second option resulted in: 2018-11-30 01:47:51 ERROR (Thread-2) [homeassistant.util.yaml] mapping values are not allowed here
in “/home/homeassistant/.homeassistant/configuration.yaml”, line 210, column 21

The first option still exhibits the problem.
Adding a delay to “Set Thermo Value” of 3 seconds seems to have helped.

My mistake, try this:

- alias: Set Thermo Value from HASS
    trigger:
      platform: state
      entity_id: input_number.thermo_slider
    condition: 
      condition: template
      value_template: '{{ states.input_number.thermo_slider.state != states.sensor.desiredtemp.state }}'
    action:
      - service: shell_command.set_thermo_value

Similarly for your first automation:

  - alias: Sync Set Thermo Value
    trigger:
      platform: state
      entity_id: sensor.desiredtemp
    condition:
      condition: template
      value_template: '{{ states.sensor.desiredtemp.state != states.input_number.thermo_slider.state }}'
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.thermo_slider
          value: "{{ states.sensor.desiredtemp.state }}"

yep:

  - alias: Sync Set Thermo Value
    trigger:
      platform: state
      entity_id: sensor.desiredtemp
    action:
      - condition: template
        value_template: '{{ states.sensor.desiredtemp.state != states.input_number.thermo_slider.state }}'
      - service: input_number.set_value
        data_template:
          entity_id: input_number.thermo_slider
          value: "{{ states.sensor.desiredtemp.state }}"

  - alias: Set Thermo Value from HASS
    trigger:
      platform: state
      entity_id: input_number.thermo_slider
    action:
      - condition: template
        value_template: '{{ states.input_number.thermo_slider.state != states.sensor.desiredtemp.state }}'
      - service: shell_command.set_thermo_value

adding "- delay: ‘00:00:03’ to the second block is helping.

I’m also not sure how to set the initial value of the slider to be states.sensor.desiredtemp.state

Still seems to be occuring even with delay. Just less often. The “wrong” value has changed now as well a few times.

I should note - the Desired Temp sensor is a command line sensor running a python script that returns the temp in a string.

Setting it is also a python script executed via “command_shell” that makes an api call.

I couldn’t use a “component” because I had to use external libraries for my python scripts and HASS python components don’t allow “import” function.