Binary_sensor, value template issue

Hi, I’m new in Homeassistant,

I’m thinkig in a binary sensor to read the status of the lights from eedomuss to integrate it in lovelace,but I’m stuck with the format of value_template.

I would appreciate if someone could help me

I’m using a command_line binary sensor calling eedomus API that works as follows:

http://192.168.0.5/api/get?api_user="MYUSER"api_secret="MYLONGSECRETPWD"&action=periph.value&periph_id=123456

answer is:

{ “success”: 1, “body”:{“last_value”: 100} }

body.last_value=100 if light is on and 0 if it’s off

I’m trying 4 diferent ways but no one works

file binary_sensors.yaml

#binary_sensor:
#trial1 
  - platform: command_line
    unique_id: sensordeestadodelaluzdearriba
    name: estado_luz_arriba
    device_class: light
    command: >
      curl -X GET "http://192.168.0.5/api/get?api_user=xxxxxx_secret=xxxxx&action=periph.value&periph_id=1334495"
    value_template: >
      "{{value_json.body.last_value }}"
    payload_on: 100
    payload_off: 0
    scan_interval: 10
#trial2
  - platform: command_line
    unique_id: sensordeestadodelaluzdearriba2
    name: estado_luz_arriba2
    device_class: light
    command: >
      curl -X GET "http://192.168.0.5/api/get?api_user=xxxxxx&api_secret=xxxxxx&action=periph.value&periph_id=1334495"
    value_template: >
      "{{value_json.body[0]["last_value"] }}"
    payload_on: 100
    payload_off: 0
    scan_interval: 10
#trial3    
  - platform: command_line
    unique_id: sensordeestadodelaluzdearriba3
    name: estado_luz_arriba3
    device_class: light
    command: >
      curl -X GET "http://192.168.0.5/api/get?api_user=xxxxx&api_secret=xxxxxx&action=periph.value&periph_id=1334495"
    value_template: >-
       {%- if 100 in state('value_json.body.last_value') -%}
         on
       {%- else -%}
         off
       {%- endif -%}
    
    #payload_on: "100"
    #payload_off: '0'
    scan_interval: '10'
#trial4
  - platform: command_line
    unique_id: sensordeestadodelaluzdearriba4
    name: estado_luz_arriba4
    device_class: light
    command: >
      curl -X GET "http://192.168.0.5/api/get?api_user=xxxxxx&api_secret=xxxxxx&action=periph.value&periph_id=1334495"
    value_template: >-
       {%- if is_state('value_json.body.last_value') == 100 -%}
         on
       {%- else -%}
         off
       {%- endif -%}
    scan_interval: '10'

Isn’t it a rest sensor you need to create?

#trial5
  - platform: command_line
    unique_id: sensordeestadodelaluzdearriba3
    name: estado_luz_arriba3
    device_class: light
    command: >
      curl -X GET "http://192.168.0.5/api/get?api_user=xxxxx&api_secret=xxxxxx&action=periph.value&periph_id=1334495"
    value_template: >
       {{ value_json.body.last_value == 100 }}

Thanks, but still not working. binary_sensor says light is off all time…

I think the issue is that I don’t really understand what does it mean

value_template: >
       {{ value_json.body.last_value == 100 }}

How is it storing the on/off state in the binary_sensor status?

It’s not storing on/off. It runs the code. If it’s true, the sensor is on, if its false the sensor is off.

Are you aware that this is a polling integration, I.e. it will only update once every 30 seconds?

Hi, to be honest I don’t know what to anwer you…

I can turn on and off lights with Homeassistant through a button in lovelace using command_line and eedomus api but I can also turn on lights with the button in the wall switch, so I need to monitor and update the status in lovelace.

I thought that with a binary sensor and shell command reading the state from the api I could update the button status with an automation

I’ve just found this error in register status

Logger: homeassistant.helpers.template
Source: helpers/template.py:575
First occurred: 16:58:00 (5 occurrences)
Last logged: 17:00:00

Error parsing value: ‘value_json’ is undefined (value: , template: {{value_json.body.last_value}})

can it be related to the name last_value from eedomus api?

best regards

Typically it means that the response is not json

Hi

Yes, I realize it has a delay time.

I think the issue is in the intrepretation of the api answer

{ “success”: 1, “body”:{“last_value”: 100} }

If I check “success” the binary sensor works fine and can detect the status 1,

  value_template: >
       {{ value_json.success }}
    payload_on: 1
    payload_off: 0

but with body.last_value it doesn’t work,

 value_template: >
    {{ value_json.body.last_value }}
    payload_on: 100
    payload_off: 0

is it because value or last_value are not usable topics?

Not sure, I can say that you have too many indents for paylod_on and pay_load off. They should be in line with value_template. And your template should be indented in relation to value_template.

  value_template: >
    {{ value_json.body.last_value }}
  payload_on: 100
  payload_off: 0