Creating a Virtual Switch in OpenHAB - Success Story!

Hey everyone,

I wanted to share my success story about creating a virtual switch in OpenHAB. I’ve been using Domoticz before, and I was really impressed by how easy it was to create virtual switches there. When I switched to OpenHAB, I wasn’t sure how to replicate that functionality, but after some research and trial and error, I figured it out!

Here’s what I did:

  1. Understanding the Need: I wanted a virtual switch that I could manually toggle from the Paper UI. This would allow me to control certain automations without needing a physical device.

  2. Research: I looked into OpenHAB’s documentation and found that the template binding was the way to go. Specifically, I needed to create a binary_sensor using the template platform.

  3. Configuration: I added the following code to my configuration.yaml file:

yaml
binary_sensor:

  • platform: template
    sensors:
    my_virtual_switch:
    friendly_name: “My Virtual Switch”
    value_template: >-
    {% if is_state(‘binary_sensor.my_virtual_switch’, ‘on’) %}
    off
    {% else %}
    on
    {% endif %}
    icon_template: >-
    {% if is_state(‘binary_sensor.my_virtual_switch’, ‘on’) %}
    mdi:toggle-switch
    {% else %}
    mdi:toggle-switch-off
    {% endif %}
  1. Testing: After restarting OpenHAB, I went to the Paper UI and was thrilled to see my virtual switch appear! I could manually toggle it, and it updated the UI instantly.

  2. Integration: I then integrated this virtual switch into my automations. For example, I set it up so that when I toggle the switch on, it turns on my living room lights and starts my favorite playlist.

I’m really happy with how this turned out. It’s a simple solution but makes a big difference in how I control my home automation setup. If anyone has questions or needs help setting something similar up, feel free to ask!

Cheers,
[Your Name]