Reversing Boolean

I could use some help on the following requirement. I have an API on my oven which returns a “true” / “false” value, depending on the state of the oven. I would like to reverse that value, so a “true” becomes a boolean “false”.

This is my sensor:

 - platform: rest
   resource: http://xx.xx.xx.xx/ai?command=getDeviceStatus
   name: 'Oven Inactive'
   value_template: '{{ value_json.Inactive}}'

My final aim is to display the oven as a switch or a light on lovelace, so that the light or switch is “on” when the oven is running.

value_template: '{{ not value_json.Inactive }}'

That works! Many thanks for your help, @anon43302295 :grinning:

1 Like

An additional question, if I may: Currently I am now getting “true” or “false” in lovelace. What would the correct and elegant way be to replace “true” with a mdi-lightbulb-on and a “false” with mdi-lightbulb for example? I looked into this, but am currently still missing too much background to understand how to implement.

Yeah, so what you would do is create a template binary sensor and display that on your frontend rather than the rest sensor.

  - platform: template
        friendly_name: Oven
        value_template: "{{ states('sensor.oven_inactive') }}" 
        icon_template: >
           {% if is_state('sensor.oven_inactive', 'on') %} mdi-lightbulb-on
           {% else %} mdi-light bulb {% endif %} 

That worked great, many thanks again for your help! Through your input, I learned about template sensors now :grinning:. The only small change I made was mdi:lightbulb-on instead of mdi-lightbulb-on.

The problem I am battling with now is that the sensor values are not updating, also not the rest platform ones. Would this be the reason and right path to pursue?

1 Like

The template sensor will update when the rest sensor does. You can use the solution to update the rest sensor at an interval of your choosing I believe.

This apparently only works if the value is boolean, which it is not in this case (value_json.Inactive returns a string). So I would first need to convert the value_json.Inactive from string to boolean. Can you give me any insight into how I can do this, so your solution would work in my case? This btw was the reason why my values were not updating, because the not always returned false…

What are the actual value_json.Inactive outputs? (and which one do you want the template to evaluate to ‘true’ on)

This is an example output from the oven api, which I was tinkering around with in the template editor, in an attempt to find a solution:

{## Imitate available variables: ##}
{% set my_test_json = {"DeviceName":"","Serial":"23072 000554","Inactive":"false","Program":" Ober-/Unterhitze,  200","Status":"Heizt auf\n","ProgramEnd":{"End":"","EndType":"0"},"deviceUuid":"0000764132"} %}

{{ my_test_json.Inactive }}

So basically it returns the string ‘false’ if the oven is on?

If so

value_template: "{{ value_json.Inactive == 'false' }}"

Will return true if the oven is on, and false if any other string is present there.

1 Like

Yes, as dumb as this may sound :slight_smile:
Many thanks, very elegant solution, I was looking at much-too-comlicated ones :wink:

PS: The oven make is a VZUG, in case anyone is looking to integrate theirs, and searching for this term.

1 Like

Your solution worked fine in the template editor. Why, when implemented in the HASS editor, is it throwing an error?

 - platform: rest
   scan_interval: 5
   name: 'Oven Active'
   value_template: '{{ value_json.Inactive == 'false' }}'
can not read an implicit mapping pair; a colon is missed at line 35, column 58:
     ... lue_json.Inactive == 'false' }}'

Because you’re using single quotes inside and outside of the template.

See my example - double quotes outside, single inside :slightly_smiling_face:

Ugh, it looks like it’s bed time for me :sleeping:
The REST part is working now, but it looks like I still need to do some work on the template sensor. In any case, I learned a lot today, many thanks to you for that! :grinning:

1 Like