Sensor command_line value template is wrong

using

  - platform: command_line
    name: Hue Daylight info
    command: !secret hue_daylight_command
    value_template: >
      {{'on' if value_json.state.daylight == 'true' else 'off'}}
    json_attributes:
      - state
      - config
      - name
      - type
      - modelid
      - manufacturername
      - swversion
    scan_interval: 300

I would have hoped to see the state to be ‘On’ right now, since this is the output of the full value_json:

{"state":{"daylight":true,"lastupdated":"2020-03-27T05:42:00"},
 "config": {"on":true,"configured":true,"sunriseoffset":15,"sunsetoffset":-45},
"name":"Daylight",
"type":"Daylight",
"modelid":"PHDL00",
"manufacturername":"Signify Netherlands B.V.",
"swversion":"1.0"}

The sensor is created alright:

and this is what it looks like in the developer-tools/template:


Please have a look what s wrong in the value_template,
thanks!

Try changing 'true' to True in the value_template. 'true' is the string true, True is the boolean value True and it looks like the attribute is a boolean, not a string.

yea, thanks, I had already tried that, based on the template I posted above, but this is still the result:

using

  value_template: >
      {{'on' if value_json.state.daylight == 'True' else 'off'}}

maybe the value_json is incorrect, will try;

  value_template: >
      {{value_json.state.daylight}}

to see what gives…

thing is, Ive made this sensor in the CC huesensor myself, and it works perfectly :+1:

def parse_phd(response: Dict[str, Any]) -> Dict[str, Any]:
    data = {}
    """Parse the json for a PHD Daylight sensor and return the data."""
    if response["type"] == "Daylight":
        daylight = response["state"]["daylight"]
        name_raw = response["name"]
        name = "Hue " + name_raw + " custom"

        if daylight is True:
            state = STATE_ON
        else:
            state = STATE_OFF
        if daylight is not None:
            data = {
                "model": "PHD",
                "state": state,
                "on": response["config"]["on"],
                "configured": response["config"]["configured"],
                "sunrise_offset": response["config"]["sunriseoffset"],
                "sunset_offset": response["config"]["sunsetoffset"],
                "name": name,
                "type": response["type"],
                "modelid": response["modelid"],
                "swversion": response["swversion"],
                "daylight": daylight,
                "last_updated": response["state"]["lastupdated"].split("T"),
                "manufacturername": response["manufacturername"]
            }
        else:
            data = {
                "model": "PHD",
                "state": "No Daylight data",
                "on": None,
                "configured": None,
                "sunrise_offset": None,
                "sunset_offset": None,
                "name": None,
                "type": None,
                "modelid": None,
                "swversion": None,
                "daylight": "No Daylight data",
                "last_updated": None,
                "manufacturername": None
            }

Remove the quotes around True. The quotes make it a string (that’s why I didn’t include them above), but that attribute is a boolean.

{{ 'on' if value_json.state.daylight == True else 'off' }}

This works for me:

value_template: > 
  {{'on' if value_json.state.daylight == true else 'off'}}


EDIT
Confirmed it also works with True (i.e. first letter capitalized; so true and True are synonyms).

of course, the quotes… thanks!

just restarted to give me this:

too bad this cant be a binary command_line sensor, because that doesnt support json_attributes listing.

maybe that will be PR’d someday :wink:
could have used:

{{ value_json.state.daylight == 'True' }}

in that case…

Right, the problem was the quotes around 'true'. Both true and True should work. In fact, this should also work:

{{ 'on' if value_json.state.daylight else 'off' }}
1 Like

The sensor is returning True instead of on now likely because you removed the quotes around 'on' and 'off'. But yes, that would work (or simply {{ value_json.state.daylight }} ). But I was under the impression you wanted it to display on and off.

So True === true === on === 1?

true == on actually helps where I have boolean template sensors that I dont want to change the previous state of when changing to unknown.

It’s a shame that strings 'on' and 'off' were adopted instead of boolean true and false to represent binary states.

In the other home automation software I use, booleans are used for binary states and it makes things neater when testing an entity’s state (like the previous template you posted).

2 Likes

Agreed. I think on and off should only be friendly names, not the actual state.

Changing that convention now would be a helluva “Breaking Change”.

Yup. Too late now at this point IMO.

I’ve grown myself a habit to check for the state in the developer-tools/state and see what the real state is, and use that in the templates, to prevent unexpected results.

thanks to you all for assisting me getting back on track!

Yup, that’s the way to do it. The “friendly name” for the state (what’s displayed in the frontend) often isn’t the actual state.

yes, as a binary_sensor template would do… return on/off based on the template being True/False :wink:

since we now can only use this as a template sensor, I need the full template.

Ok so what I wrote isn’t true. on != true according to the template editor. So I’m still stuck making a binary template sensor ignore the unknown state. e.g.

- platform: template
  sensors: 
    gate_filtered:
      name: 'Gate filtered' # no unknown state when device asleep
      device_class: door
      value_template: >
        {% if is_state('binary_sensor.gate', 'on') %}
          {{ true }}
        {% elif is_state('binary_sensor.gate', 'off')%}
          {{ false }}
        {% else %}
          {{states('binary_sensor.gate_filtered') }}
        {% endif %}

The else case is on/off not true/false so it does not work. I cant see a way to make the sensor retain the previous state before going unknown.