JSON to binary sensor template issue

I have a REST sensor that pulls JSON data;

chargingstation: 
type: UNKNOWN
charging_when_closed: true
time_zone: Europe/Amsterdam
support:
  tel: +(31)-(900)-2273427
  email: [email protected]
  twitter_webcare: 'https://twitter.com/greenfluxinfo'
  facebook_page: 'https://www.facebook.com/greenfluxinfo'
chargingspots:
  - uid: NL-GFX-EPP002559-1
    ocpi_evse_id: NL-GFX-EPP002559-1
    physical_reference: NL-GFX-EPP002559
    status: AVAILABLE
    capabilities:
      - CHARGING_PROFILE_CAPABLE
    parking_restrictions: EV_ONLY
    connectors:
      - id: '1'
        standard: IEC_62196_T2
        format: SOCKET
        power_type: AC_3_PHASE
        voltage: 230
        amperage: 16
        max_power: 11040
        tariffs: []
  - uid: NL-GFX-EPP002559-2
    ocpi_evse_id: NL-GFX-EPP002559-2
    physical_reference: NL-GFX-EPP002559
    status: CHARGING
    capabilities:
      - CHARGING_PROFILE_CAPABLE
    parking_restrictions: EV_ONLY
    connectors:
      - id: '2'
        standard: IEC_62196_T2
        format: SOCKET
        power_type: AC_3_PHASE
        voltage: 230
        amperage: 16
        max_power: 11040
        tariffs: []

There are 2 ID’s (chargingspots) in this.
I have a template sensor for both:

value_template: '{{ states.sensor.ev_charging_station_sensors.attributes["chargingstation"]["chargingspots"][0]["status"]}}'
value_template: '{{ states.sensor.ev_charging_station_sensors.attributes["chargingstation"]["chargingspots"][0]["status"]}}'

This results in:

  • CHARGING
  • AVAILABLE

Now I want to change it into a binary sensor, but I cannot make this happen.

I tried:

{{ is_state_attr("sensor.laadpaal_ev_charging_station_sensors.chargingstation.chargingspots.0", "status", "AVAILABLE") }}
        value_template: >-
          {%- if is_state('sensor.laadpaal_ev_charging_station_sensors.attributes["chargingstation"]["chargingspots"][1]["status"]', 'CHARGING') -%}
          On
          {%- else -%}
          Off
          {%- endif -%}

Both do not translate into true/false

Anybody an idea? It must be a simple syntax thing i think??

Try this:

"{{ state_attr('sensor.laadpaal_ev_charging_station_sensors', 'chargingstation')['chargingspots'][0]['status'] == 'AVAILABLE' }}"

You can also always try it out under Developer Tools -> Templates.

@Burningstone :see_no_evil:

I tried in the template tool, over and over…
But every option I tried resulted in “None” or an error :-S

like

{{ is_state ("sensor.ev_charging_station_sensors.attributes.chargingstation.chargingspots.1.status", "CHARGING")}}

results in None.

10 minutes ago I came up with this one… that works…

{{ 'CHARGING' in state_attr("sensor.ev_charging_station_sensors", 'chargingstation')['chargingspots'][1]['status']}}

But yours is way more straight and none cluttered.
Thank you for your effort!

1 Like