Switch that should show the state of a binary_sensor

Heya I created a virtual switch that is running a script.
What I still trying to figure out is on how to change the state of the switch by the state of a binary_sensor:

- platform: template
  switches:
    markise:
      value_template: "{{ is_state('binary_sensor.wohnzimmer_sensor-markise_contact','on') }}"
      turn_on:
        service: script.turn_on
        target:
          entity_id: script.logikmarkise
      turn_off:
        service: script.turn_on
        target:
          entity_id: script.logikmarkise
      icon_template: >-
        {% if is_state('binary_sensor.wohnzimmer_sensor-markise_contact','on') %}
          mdi:blinds
        {% else %}
          mdi:blinds-open
        {% endif %}

right now the switch isn’t changing it’s value at all, it always stays off

What sets the binary_sensor to on?

it is a physical zigbee device that outputs on or off as the state of its own sensor

When you execute this:

        service: script.turn_on
        target:
          entity_id: script.logikmarkise

Does the Zigbee device’s state change to on?

If executing the script fails to make the device change to on then the Template Switch will continue to report off.

If do your small snipped inside the devtools my sensor turns from off to on and if I call the script again it turns off after the blind is fully closed

here from the history view:

Go to Developer Tools > States and find the binary_sensor in the Entity column. Confirm the spelling of its entity_id. Check if it actually contains a hyphen in the entity_id.

binary_sensor.wohnzimmer_sensor-markise_contact
                               ^
                               |
1 Like

it does, you can see the chosen entity above in my screen with the hyphen, but here again a screen directly from the entity page:

image

EDIT: ah… the friednly name is the problem, I see what you mean now :slight_smile:
EDIT2: tested and confirmed, working now… god damn, I will never use hyphens again!

1 Like

An entity_id contains only letters in lowercase, numbers and underscores (it’s known by the curious name ‘slug’ and there’s even a template function called slugify to convert text to a slug).

Your Template Sensor was configured to get its state from a binary_sensor that didn’t exist in your system (because the entity_id was misspelled).

For future reference, rely on Developer Tools > States to display an entity’s correct entity_id and friendly_name.

You can use hyphens in an entity’s friendly_name provided you remember that it will be converted to an underscore for the entity’s entity_id.

1 Like