Giter Site home page Giter Site logo

Switch state in conditions ? about esphome HOT 10 CLOSED

esphome avatar esphome commented on May 12, 2024
Switch state in conditions ?

from esphome.

Comments (10)

brandond avatar brandond commented on May 12, 2024

I think the current recommended way to do this is to:

  • Set up the LED as a binary light, but don't give it a name (to prevent it from showing up in HASS)
  • Set up the relay as a switch, also without a name
  • Set up a template switch, configure it to turn the LED and relay on and off together

from esphome.

OttoWinter avatar OttoWinter commented on May 12, 2024

Yes, as @brandond said: currently that's only possible with a template switch. Kind of like this:

# Pseudocode!
switch:
  - platform: gpio
    id: led
    pin: D4
  - platform: gpio
    id: relay
    pin: D5
  - platform: template
    name: My Switch
    turn_on_action:
        - switch.turn_on:
            id: led
        - switch.turn_on:
            id: relay
    turn_off_action:
        - switch.turn_off:
            id: led
        - switch.turn_off:
            id: relay

As that's IMO pretty easy, I'm not sure I should add native support for it (would just end up cluttering docs). But for the "triggering on a switch", I guess that would be possible, might take a look at that once I get some time.

from esphome.

TheJulianJES avatar TheJulianJES commented on May 12, 2024

Yeah, I hoped something like this would work:

binary_sensor:
  - platform: gpio
    id: button
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle:
            id: relay

switch:
  - platform: gpio
    id: relay
    name: "Sonoff3"
    pin: GPIO12
    turn_on_action:
      then:
        - light.turn_on:
          id: led_light
    turn_off_action:
      then:
        - light.turn_off:
          id: led_light

output:
  - platform: esp8266_pwm
    id: basic_green_led
    pin:
      number: GPIO13
      inverted: True

light:
  - platform: monochromatic
    id: led_light
    output: basic_green_led

The template switch method seems to work though. If implementing the turn_on//-off actions in the (gpio) switch (or switch core?) is possible, then that would be wonderful!

from esphome.

OttoWinter avatar OttoWinter commented on May 12, 2024

@TheJulianJES Why not something like this?

binary_sensor:
  - platform: gpio
    id: button
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - switch.toggle:
            id: relay
        - light.toggle:
            id: led_light

from esphome.

TheJulianJES avatar TheJulianJES commented on May 12, 2024

Yeah sorry, I should have updated that comment. I already got that solution on discord although I previously tried something that worked but spammed mqtt, even after a slimmed down version (which doesn't work but shouldn't spam mqtt?): https://i.imgur.com/0jE3xCt.png // https://i.imgur.com/0DlpKZH.png (Maybe a bug?) https://hastebin.com/zocicupata.m was another solution I came up with but that might have been a bit overcomplicated. So, then somebody sent an option like yours on Discord (makes sense, but I didn't think of that for some reason).

from esphome.

vincegre avatar vincegre commented on May 12, 2024

binary_sensor:

  • platform: gpio
    id: button
    pin:
    number: GPIO0
    mode: INPUT_PULLUP
    inverted: True
    on_press:
    then:
    - switch.toggle:
    id: relay
    - light.toggle:
    id: led_light

Nice but not fault proof as if led_light has been toggled for an other reason light will be then in opposite state ! Would be better and more fault proof to put light in same status as relay ;)

from esphome.

michamichamicha avatar michamichamicha commented on May 12, 2024

I tried using the led as power_supply option but that does not work for me. Not sure why but could also be an option?

from esphome.

vincegre avatar vincegre commented on May 12, 2024

I tried using the led as power_supply option but that does not work for me. Not sure why but could also be an option?

What are you trying to do exactly ?

from esphome.

michamichamicha avatar michamichamicha commented on May 12, 2024

I'm trying to do the exact same thing as you are :) Making the led reflect the state of the relay.
I tried using the power_supply option, so the board would enable the led as if it were the power supply for the relay. This would also allow it to shut it down after the relay has been disabled.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    id: bogus_2
    on_press:
      then:
        - light.toggle:
            id: living_lights_front_2

power_supply:
  - id: basic_green_led
    pin:
      number: GPIO13
      inverted: True
    keep_on_time: 0.5s

output:
  - platform: gpio
    pin:
      number: GPIO12
    id: 'generic_out'
    power_supply: basic_green_led

light:
  - platform: binary
    name: "Living room lights front 2"
    output: generic_out
    id: 'living_lights_front_2'

But for some reason it's not working. Still, this is a workaround and it would be nice if we can make multiple outputs work in sync. Maybe this can be done by setting the correct MQTT payload topic? Not sure if it's available for the light component.

from esphome.

OttoWinter avatar OttoWinter commented on May 12, 2024

this should now be possible with the latest automation engine changes.

from esphome.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.