Current sensor to show state of lamp and be able to trigger the switch On or Off

Hello all,
I have a voltage sensor that detects the state of a lamp ON or OFF. I want this to show up under my light menu in HA but with the switch command having the ability to toggle the actual lamp ON and OFF while keeping the lamp status in sync.
To further complicate my request I have my lamp wired for 2 way switching, 1 from my wall switch and 2 from my relays wired to rpi gpio.

I have really been confused with this and would appreciate any assistance I can get. Thanks in advance

You need to define a template switch. Then convert that switch to a light using the light-switch integration.

As long as you get the value template correct (voltage > ?) in the template switch its state will follow switching from either the relay or physical switch.

thanks Tom
So my voltage detector feeds the rpi gpio so I only get On Off no actual voltage readouts,which is still ok.
I have a template switch that is linked to the state of the voltage detector. I then convert this using the light switch command.
Now I can can see the on off as a lamp icon, but is there anyway I can use the toggle part to be linked to activate my relay side for the actual switch?
thanks

Absolutely. That’s what the turn_on and turn_off action options are for.

Look at the examples. If you cant work it out post your relay entity_id.

# binary sensor config
binary_sensor:
  - platform: remote_rpi_gpio
    host: 192.168.1.195
    scan_interval: 10
    ports:
      12: V1
# relay config
switch:
  - platform: remote_rpi_gpio
    host: 192.168.1.195
    invert_logic: true
    ports:
      03: Relay Kitchen
# switch template config
switch:
  - platform: template
    switches:
      kitchen:
        value_template: "{{ is_state('binary_sensor.v1', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.relay_kitchen
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.relay_kitchen
# light config
light:
  - platform: switch
    name: Kitchen Light
    entity_id: switch.kitchen

This should be correct but its not behaving as it should. Ideally if my sensor triggers then by my light config the bulb should be on or off depending on the state of the sensor. The switch config should not toggle the relay but only do so when I toggle it.
I hope I have the correct understanding because what I am trying to do is tell the state of the lamp regardless if my physical switch is pressed and there by be able to not disrupt mess it up when I toggle the relay.
thanks much tom you have shed a lot of light on situation

That all looks correct.

Not sure what you mean by “mess up”.

Your switch will react to changes made in home assistant or by the physical switch as you have a voltage monitor for feedback.

The problem may be that you have not correctly wired the relay and physical switch to both be able to control the light. The switch and relay can not be simply wired in series, or in parallel. It requires this configuration:

Thanks very much,

Messed up means mixed states so my logic don’t get messed up as it is getting mixed up.
I have 2 way wiring but a bit different from yours

That is essentially the same wiring. So you’re good to go. You should be able to control the light from either the switch or relay.

So I had a chance to check it and it does not work as it should at all. The Light config is able to see the correct state of the bulb when it is ON/OFF. But the slider does not do anything at all to the relay to control it from under the Light card. Similarly the Template command is also not able to change the state when I move the slider slider either. Form the above the only control I get is using the switch command for the relay only.

What slider?

It’s not a dimmer it’s a switch (relay).

sorry the switch tab that moves like a slider on off.

agreed its not a dimmer.

I just dont understand what im missing to get the on off to work in my template or light cards

Made some more checks and I have no control on either the switch template or my light. The lamp icon on the light works flawless with the light activation and the switch corresponds with the light icon status. But if I try to move the switch to off if the light is on, it goes to the off position momentarily and then switches back on. What am I missing that will allow me to get control of the light and not have it stay locked to what ever state the voltage sensors is in?

This is used to determine the state of the switch:

value_template: "{{ is_state('binary_sensor.v1', 'on') }}"

Checking that would be a good place to start.

This is used to change the state of the switch:

        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.relay_kitchen
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.relay_kitchen

So check that as well.

hello again Tom

So check through everything and realized because im using 2 way switching I needed to change my command from Switch to a Toggle and it all works fine.

Seems however that my sensors that are connected to my raspbian pi directly on GPIO pins are unable to be reliably detected by REMOTE pi command from my HA running on my next PI 3. If i encounter a power failure im stuck with my sensors not being detected and sometimes end up having to do multiple shutdown of both rasbian and HA pi and hope they play together. This is my new nightmare
Two questions come to mind as many have said their remote Gpio detection works flawless so is that because they are using the HA on docker on a virtual environment?
I have been looking at flyt method over mqtt and wanted to know if that may be a more reliable and viable method

thanks much again to all for the assistance

I’ve used flyte’s gpio to mqtt app on two raspis (nearly all gpio) faultlessly for years.

don’t have to tell me twice taking the dive now

Did a fresh install of latest raspbian buster
configured relevant settings remote gpio,ssl, location etc

python --version

showed ver 2.7 so ran

nano ~/.bashrc

then edited with

alias python='/usr/bin/python3'

did below to reset

source ~/.bashrc

then a recheck of python version and showed 3.7
then used

sudo apt-get update
sudo apt-get install python3-pip

then edited to make python3 default with below

alias pip=pip3

then tried to add flyte items with

pip install pi-mqtt-gpio

Got the below error

Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://www.piwheels.org/simple/cerberus/

So did and upgrade with

pip install -U pip

retried flyte package and was able to download all the items including Cerberus but i got a new error

WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with ‘-m pip’ instead of running pip directly.
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pi-mqtt-gpio

AND then this

WARNING: The script pi_mqtt_gpio is installed in ‘/home/pi/.local/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

I want to start building but I would like to know if you encountered any of these errors or how are you getting around the install without them being a problem?

thanks again

I followed the VM install instructions on flyte’s github. This was the only issue I had:

I removed pip latest version and did a reinstall of flyte command and no errors and all went well.

I cannot seem to find any instructions as to where the config.yaml file must reside on the rasbian pi.
I require some assistance on this part

So I am trying to create a file in my usr/bin/python folder and its being denied
The software part on the pi and navigating and creating files and folders are clearly my challenges.