Automation with esp/MQTT

I have the following sensor sending the temp over mqtt to the broker which runs on the same pi as HA.

  • platform: mqtt
    state_topic: “ha/in”
    name: esp0
    unit_of_measurement: “C”
    value_template: ‘{{ value_json.svalue }}’

I would like to change the color of the leds by automating it within HA if the temperature rises above 15c.
Though i’m unable to get this right.

  • alias: Above 15
    trigger:
    platform: state
    entity_id: sensor.esp0
    condition:
    - condition: template
    value_template: ‘{{ value_json.svalue < 15 }}’
    action:
    service: shell_command.set_candle_above

  • shell_command:
    set_candle_above: ‘curl --data “cmd=CANDLE:3:FF0000:200” http://192.168.0.14/control

I’m getting the following error:

17-04-27 16:06:20 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘value_json’ is undefined

What am i doing wrong ?

As the error says, ‘value_json’ is not available in the automation.
You have to work with your sensor, so
value_template: '{{ states.sensor.esp0.state | int < 15 }}'
should work.

You’ll want to look at the state of sensor.esp0 and use a numeric trigger. Something like this should work:

  trigger:
    platform: numeric_state
    entity_id: sensor.esp0
    above: 15

Thank you both.
I’m getting close since i’m not getting any errors now , though it’s not executing the shell_command.

While data is coming in true the mqtt

{“idx”:123,“nvalue”:0,“svalue”:“21.06”}

I tried

value_template: ‘{{ states.sensor.esp0.state }}’

and

value_template: ‘{{ states.sensor.esp0.state > 15 }}’

getting the following message in logbook.

Above 15 turned on

When i press Above 15 I can trigger it manually. this works.
Any clue points here ?

I would try it without the double quotes in the shell command or maybe put | int as the value is a float.

This is what I have now.

  - alias: red
    trigger:
      platform: numeric_state
      entity_id: sensor.esp0
      above: 20
    action:
      service: shell_command.set_candle_above

  - alias: green
    trigger:
      platform: numeric_state
      entity_id: sensor.esp0
      below: 20
    action:
      service: shell_command.set_candle_below

But still not working.
Would be nice if I could debug the triggers somehow.

The ‘svalue’ in the mqtt json payload is a string.
So you can convert it in your sensor
value_template: '{{ value_json.svalue | int }}' (float should already work)
and use the numeric_state trigger, or you use the template trigger as i suggested in my first post.

I already have the value_template converted to a int
like

  - platform: mqtt
    state_topic: "ha/in"
    name: esp0
    unit_of_measurement: "°C"
    value_template: '{{ value_json.svalue | int }}'

I don’t understand the part where you say I could use a template trigger.
If you mean like using it like this:

  - alias: red
    trigger:
      platform: state
      entity_id: sensor.esp0
    condition:
      - condition: template
        value_template: '{{ value_json.svalue | int < 15 }}'
    action:
      service: shell_command.set_candle_above

it’s not working either

If your sensor returns a number, you can use the numeric_state trigger.

  - alias: red
    trigger:
      platform: numeric_state
      entity_id: sensor.esp0
      above: 20
    action:
      service: shell_command.set_candle_above

If it returns a string, you can convert the sensor value to a number, or you can use the template trigger

  - alias: red
    trigger:
      platform: template
      value_template: '{{ states.sensor.esp0.state | int < 15 }}'
    action:
      service: shell_command.set_candle_above

Both will only trigger if the value changes. Maybe this is your problem.

1 Like

Finally it’s working. Sorry for not understanding the first time.
I’m using the second option you posted.

  - alias: green
    trigger:
      platform: template
      value_template: '{{ states.sensor.esp0.state | int < 23 }}'
    action:
      service: shell_command.set_candle_below

  - alias: red
    trigger:
      platform: template
      value_template: '{{ states.sensor.esp0.state | int > 23 }}'
    action:
      service: shell_command.set_candle_above

Thank you :blush: