Value_template condition

Hello,

I’m working in ESP-Tasmota as Marlin sniffer · Discussion #11770 · arendst/Tasmota · GitHub
The serial briedge to mqtt works. Now the problem is in the sensor conditional configuration, because I receive this type of commands:

  • "SerialReceived":"echo:Print time: 0s\nok\n"
  • "SerialReceived":"ok T:15.43 /0.00 B:15.47 /0.00 @:0 B@:0\n"
  • "SerialReceived":"Not SD printing\nok\n"

My idea was configure in config.yalm one sensor for each parameter, something like this:

- platform: mqtt
    name: "Printing Time"
    state_topic: "tele/3dprint/RESULT"
    value_template: >
      {% if '{{ value_json["SerialReceived"].split(":")[1] }}' == 'Print time' %}
        {{ value_json["SerialReceived"].split(":")[2].split("\n")[0] }}
      {% endif %}
    force_update: true 

The split works well, but the comparison operation ‘==’ didn’t work. I’ve tried differents ways:

  • {% if states('{{ value_json["SerialReceived"].split(":")[1] }}', 'Print time') %}
  • {% if '{{ value_json[SerialReceived].split(:)["1"] }}', 'Print time' %}
  • {% if '{{ value_json[SerialReceived].split(:)["1"] }}' == 'Print time' %}
    And any one work properly, I supose it’s a typologi issue, or it’s gramatical?

Thank you in advance

    value_template: >
      {% if value_json["SerialReceived"].split(":")[1] == 'Print time' %}
        {{ value_json["SerialReceived"].split(":")[2].split("\n")[0] }}
      {% else %} 
        N/A
      {% endif %}

Ok, thank you, it works now!!

Just need to add {{ N/A }

Not sure what you mean by this?

I mean, in the else answer, when I put just N/A the answer was N/A; but if you put {{ N/A }} the answer is nothing, the old value stay. At least in my settup

Putting things in {{ }} will force them to be evaluated as a template. {{ N/A }} is actually calculating the division of non existent string N by non existent string A. I would be very surprised if this has not generated an error in your log.

Some better options:

      {% else %} 
        none
      {% endif %}
      {% else %} 
        unknown
      {% endif %}
      {% else %} 
        0
      {% endif %}
      {% else %} 
        -
      {% endif %}

is it possible to only trigger a value change if the if statement is fulfiled?
if i remove “else” it resets the value to an empty value. is there an option to discard the change and just trigger the values if there are any matching?

You can use this to keep the state the same in the else case:

      {% else %} 
        {{ this.state }}
      {% endif %}
1 Like