Change how dummy switch is displayed in Lovelace

Hi,

I want to display the status of a NodeRED flow in Lovelace. So in NodeRED this “dummy switch” will be switched on and off when the flow is running.

To make this happen, I have added the following code to configuration.yaml:

input_boolean:
  aanwezigheidssimulatie_status:
    name: Status Aanwezigheidssimulatie
    initial: off
    icon: mdi:shield-home

This entity is displayed in Lovelace like a normal switch, like this:


However, I am looking for a solution to display it with “on” and “off”, so that it is not possible to change the status, but that the status is displayed in Lovelace. See the example below which I found on internet:

616e4c7fb64698f02206351f464645cb749d9c73
Does anybody have a solution for this? Thanks in advance for your support!

1 Like

You can use an API node to create / update a sensor with a POST request to /api/states/entityid. No need for an input_boolean.

Hi, thanks for your reaction. Can you please give me an example as I’m a newbie in the Home Assistant world.

Function node to create the payload, adapt to your needs.

[{"id":"83f15108.74e64","type":"function","z":"9774f299.39eb8","name":"format entity","func":"msg.entity_id = \"sensor.warning1\";\nvar nr = 0;\n\nif(msg.payload.length > nr) { \n\n  msg.payload = {\n    data: {\n        state: \"on\",\n        attributes: {\n            start:       msg.payload[nr].start,\n            end:         msg.payload[nr].end,\n            active:      msg.payload[nr].active,\n            color:       msg.payload[nr].color,\n            icon:        msg.payload[nr].icon,\n            headline:    msg.payload[nr].headline,\n            description: msg.payload[nr].description,\n            instruction: msg.payload[nr].instruction,\n            sender:      msg.payload[nr].sender,\n            location:    msg.payload[nr].location\n        }\n    }\n  };\n}\nelse {\n  msg.payload = {\n    data: {\n        state: \"off\",\n        attributes: {}\n    }\n  };\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":550,"y":180,"wires":[["532e10c.69c63f"]]}]

API node, see node documentation to create an access token for HA

[{"id":"532e10c.69c63f","type":"ha-api","z":"9774f299.39eb8","name":"","server":"2b393d9c.07d642","protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","location":"payload","locationType":"msg","responseType":"json","x":730,"y":180,"wires":[[]]},{"id":"2b393d9c.07d642","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true}]

Connect the function node to the API node.

Or, if you can send a static payload for the sensor creation / update, configure that in the API node.

An API call to update an entity will create the entity if it does not exist. That is quite elegant.

Hi Holger,

I have found another solution for this issue. I have added the following code to configuration.yaml to create a “input_select dummy sensor” which is used to display the status of a NodeRED flow in Lovelace.

Thanks for your help!

sensor:
  - platform: template
    sensors:
      aanwezigheidssimulatie_status:
        value_template: '{{ states.input_select.aanwezigheidssimulatie_status.state}}'

input_select:
  aanwezigheidssimulatie_status:
    name: Status Aanwezigheidssimulatie
    options:
      - Uit
      - Aan
    initial: Uit

Recent versions of the node-red-contrib-home-assistant-websocket, allow you to define and update sensors and binary sensors directly into home assistant via a simple node.

1 Like

Hi, Thank you for the information. Very helpful!