Customize Value of Input Boolean entity

So I have defined the following entities in the configuration.yaml

input_boolean:
  skylink_basement_sensor:
    name: Basement Sensor
    initial: off
    icon: mdi:walk
  skylink_side_door:
    name: Side Entrance Door
    initial: off
    icon: mdi:door-closed

It shows up as the following in the interface:

image

Is there a way to change the behavior so it would say “Detected”, “Clear” or “Open”, “Closed” like the other sensors?

Is this a interface config or yaml config?

Thank you for any guidance on this!

The other sensors are binary_sensors. A binary_sensor is rendered with its status whereas an input_boolean is rendered as a toggle button.

An input Boolean is on/off or true/false.

Your other items are sensors…you can’t have an input Boolean be a sensor.

Could you please explain why you are using an input boolean for these two? I assume “Basement Sensor” is a motion sensor? What do you want to turn on/off?

I agree, it’s a bit unclear what you’re trying to do here. But, FYI, you can create a Template Binary Sensor that shows the current state of an input_boolean, and you can set the device_class to control how the states are displayed. Then you can either hide or display the input_boolean in the frontend depending on how you’re using it.

For example:

binary_sensor:
  - platform: template
    sensors:
      skylink_side_door:
        friendly_name: Side Entrance Door
        icon_template: >
          {% if is_state('input_boolean.skylink_side_door', 'on') %}
            mdi:door-open
          {% else %}
            mdi:door-closed
          {% endif %}
        device_class: door
        value_template: "{{ is_state('input_boolean.skylink_side_door', 'on') }}"
2 Likes

Hi Everyone! Thank you very much for your input on this. To make things clear, my approach being incorrect is due to my lack of knowledge in this space. I am not a developer and am trying to consume a lot of information.

I have a Skylink Alarm System (this doesn’t have Google or HA integration but works with IFTTT). It has the following setup.

I am trying to create these sensors in HA using IFTTT webhook. I started this by trying to create HTTP sensors using IFTTT. But kept getting 401 errors from IFTTT and authentication errors in HA. Then read led me to the Input Boolean, which functionality wise works but as everyone pointed out here is not the right approach.

So I will create the binary sensor using the template platform. I may have some followup questions. Thanks for being patient and your replies.

1 Like

So it’s possible to create a state in HA’s State Machine (that doesn’t correspond to an actual entity) via a Python Script, just like you can via the HTTP Binary Sensor. Then you can call this script from an automation that responds to an IFTTT webhook received event, effectively cutting out the input_boolean “middleman.”

Let me know if you’re interested, and I can fill you in on the details.

yes please!!! I would like to learn. I need to learn Python scripts too. If you can provide some help, very appreciated.

Ok, step 1. :slightly_smiling_face:

Add the following line to your configuration:

python_script:

Next add a folder named python_scripts into your config directory.

Next add a file named set_state.py to that folder (i.e., config/python_scripts) with the following content:

if 'entity_id' not in data:
    logger.warning("===== entity_id is required if you want to set something.")
else:
    data = data.copy()
    inputEntity = data.pop('entity_id')
    inputStateObject = hass.states.get(inputEntity)
    if inputStateObject:
        inputState = inputStateObject.state
        inputAttributesObject = inputStateObject.attributes.copy()
    else:
        inputState = 'unknown'
        inputAttributesObject = {}
    if 'state' in data:
        inputState = data.pop('state')
    logger.debug("===== new attrs: {}".format(data))
    inputAttributesObject.update(data)

    hass.states.set(inputEntity, inputState, inputAttributesObject)

Restart HA.

Now you have a new service named python_script.set_state. If you want to play with it you can call it on the SERVICES tab of the Developer Tools page, something like this:

image

In a script or in the action part of an automation you would call it like this:

- service: python_script.set_state
  data:
    entity_id: binary_sensor.skylink_side_door
    friendly_name: Side Entrance Door
    icon: mdi:door-open
    device_class: door
    state: 'on'

And, of course, you can use data_template instead of data in an automation and use templates as needed in the various parameters to set the right entity_id, state and attributes based on what you receive in the IFTTT webhook received event. I can help more with that, too, if you like. But I’d suggest doing the above first to get comfortable with it. :slightly_smiling_face: Let me know if you have any questions.

EDIT: BTW, I should state explicitly, the entity_id parameter is required. The state parameter is optional. If present it will set the entity to that state. If not, it will leave the state as-is, or set it to 'unknown' if the entity did not yet exist in the State Machine. All other parameters become attributes.

1 Like

Thank you for this direction. I have setup the above and now am able to call the service from the service section in HA.

In the configuration.yaml, I have the binary sensor defined as follows.

binary_sensor:
 - platform: template
   sensors:
      skylink_side_door:
        friendly_name: Side Entrance Door
        icon_template: >
          {% if is_state('input_boolean.skylink_side_door', 'on') %}
            mdi:door-open
          {% else %}
            mdi:door-closed
          {% endif %}
        device_class: door
        value_template: "{{ is_state('input_boolean.skylink_side_door', 'on') }}"

Since I no longer need the input_boolean, do I define the above template sensor as follows?

binary_sensor:
 - platform: template
   sensors:
      skylink_side_door:
        friendly_name: Side Entrance Door
        device_class: door

2nd question, for the body of the IFTTT webhook, I have the following:

{ "action": "call_service", "service": "input_boolean.turn_on", "entity_id": "input_boolean.skylink_side_door" }

should this be changed to?

{ "action": "call_service", "service": "python_script.set_state", "entity_id": "binary_sensor.skylink_side_door", "state":"on" }

You no longer need either the input_boolean, or the template binary sensor. You’ll use python_script.set_state to (effectively) create binary_sensor.skylink_side_door.

If this doesn’t make sense, then you should probably give Home Assistant Architecure a read, especially the diagram at the bottom of the page labeled “Home Assistant Core Architecture.”

python_script.set_state is writing directly to the State Machine. You don’t need any actual components. Pretty much everything in HA (frontend, services, etc.) interacts with the State Machine via the Event Bus. If you write something to the State Machine directly, it’s the same as if a component did it. BTW, when you change the state of an “entity” at the top of the STATES tab on the Developer Tools page, that’s what you’re doing – i.e., writing directly to the State Machine. That’s why it says, “Set the representation of a device within Home Assistant. This will not communicate with the actual device.”

That’s one way to do it. But if you do, you should also specify the friendly_name, icon & device_class attributes.

A better way might be to define a new action, and then specify only what’s unique about that particular applet. Then write a new automation that listens for that action and pulls out the details it needs from the event data, and then fills in the rest (that doesn’t change) in the call to python_script.set_state.

ok I understand this piece. I took out all the sensors and binary sensors in the configuration.yaml and restarted HA. Then called the service. After that I can see entity in the State selection list under developer tool.

Seems like the entities created by this goes away when HA restarts, correct? as it is a machine state entity?

1 Like

Yes, that is correct. You had said you tried the HTTP Binary Sensor, which also disappears when HA restarts, so I assumed this would not be an issue for you. But if you need the state to be saved through a HA restart, then using the input_boolean is the right way to do that.

1 Like

Understood. Yes, I want it to stick around but the confusion is because of my lack of knowledge.

Thank you for all your help with this. I learn something new about the python service and how to call it. So this has been truly helpful!

1 Like

Btw, so since I am creating the template sensor (binary sensor) and changing the value to the input boolean from IFTTT, is it possible to set the state of the template sensor (binary sensor) directly from IFTTT instead of changing the input boolean?

ah nevermind, so the binary sensors are read only…so input boolean is the way to go for these. Thanks!

Correct. The template binary sensor is only to get the state of the input_boolean to display the way you want it. (I have similar things in my config.) The input_boolean is a “behind the scenes” implementation detail. :slightly_smiling_face: