Restful, value template help please

Hello. I would like to ask for some help with restful integration, moxa iologik and value templating. I’ve been trying all day but I surely miss something.

First of all, I want to use a Moxa Iologik e1212 as a general i/o modul for my home assistant system.

I tought that restful integration will be the easy way for doing this. For outputs it was, and controlling outputs is working fine.

But when I want to see inputs in home assistant there comes the problem.
I need to use some templating since the answer I get from the modul is not clear boolean or on/off.

This is the message I get when I ask the input status:

{“slot”:0,“io”:{“di”:{“0”:{“diStatus”:0}}}}

So I wanted to use this template at the rest binary sensor:

value_template: ‘{{value_json[‘io’][‘di’][‘0’][‘diStatus’]}}’

Binary sensor config:

- platform: rest
  resource: http://192.168.1.126/api/slot/0/io/di/0/diStatus
  value_template: '{{value_json['io']['di']['0']['diStatus']}}'
  method: GET
  name: MOXA_di_0
  headers:
     Content-Type: application/json
     Accept: vdn.dac.v1

But when I check config I allway get an error

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/bin_sensor.yaml", line 8, column 3
expected <block end>, but found '<scalar>'
  in "/config/bin_sensor.yaml", line 10, column 34

8/3 is the platform line and 10/37 is value_template line at [‘io’]

I just don’t know what is the problem.

Can someone help me, please?

change the outside quotes to “”

- platform: rest
  resource: http://192.168.1.126/api/slot/0/io/di/0/diStatus
  value_template: "{{value_json['io']['di']['0']['diStatus']}}"
  method: GET
  name: MOXA_di_0
  headers:
     Content-Type: application/json
     Accept: vdn.dac.v1

Try to encapsulate the whole value_template in single quotes and replace the single quotes in your square brackets with double quotes:

value_template: '{{value_json["io"]["di"]["0"]["diStatus"]}}'

The other thing might be that this will return a string of 0 (or 1). As it is a binary sensor, does this need to be tested to return true or false in the template?

1 Like

Yes, this is the next step I have to do.
Do you know maybe how to translate 0/1 to false/true?

You shouldn’t need to, unless that’s the state you want. Just place the whole sensor into the binary_sensor catagory instead of sensor and it should just work itself out. The state will be on/off.

binary_sensor:
- platform: rest
  resource: http://192.168.1.126/api/slot/0/io/di/0/diStatus
  value_template: "{{value_json['io']['di']['0']['diStatus']}}"
  method: GET
  name: MOXA_di_0
  headers:
     Content-Type: application/json
     Accept: vdn.dac.v1

It’s already undery binary sensors.
But the entity doesn’t change state :frowning:

Well, okay I was the dumb. I was looking wrong entity :confused:
Everything works now :slight_smile: Thanks petro!

1 Like