找回密码
 立即注册

微信扫码登录

搜索
查看: 2045|回复: 3

[技术探讨] 磁保持继电器插座LED灯状态显示的问题

[复制链接]

46

主题

162

回帖

969

积分

高级会员

积分
969
金钱
761
HASS币
0
发表于 2024-4-26 19:04:34 | 显示全部楼层 |阅读模式
插座只有一个led灯,希望可以显示连接状态,也可以显示继电器状态。


代码如下:
output:
  - platform: gpio
    id: led1
    pin:
      number: GPIO2
      inverted: True

light:
  - platform: status_led
    name: "Switch state"
    id: status_led1
    output: led1
    internal: True

switch:
  - platform: gpio
    name: "电源开关"
    pin: GPIO13
    inverted: true
    id: relay
    restore_mode: RESTORE_DEFAULT_ON
    on_turn_on:
      - output.turn_on: led1
      - switch.turn_off: ina
      - switch.turn_on: inb
      - delay: 100ms
      - switch.turn_off: ina
      - switch.turn_off: inb
      - delay: 100ms
    on_turn_off:
      - output.turn_off: led1
      - switch.turn_on: ina
      - switch.turn_off: inb
      - delay: 100ms
      - switch.turn_off: ina
      - switch.turn_off: inb
      - delay: 100ms
    
  - platform: gpio
    pin: GPIO4
    id: ina
  - platform: gpio
    pin: GPIO5
    id: inb


但是发现,有时候断电再上电,LED状态和继电器状态并不对应。

于是改了下代码,每隔5秒查询一次继电器状态,再更改LED状态,代码如下:

output:
  - platform: gpio
    id: led1
    pin:
      number: GPIO2
      inverted: True

light:
  - platform: status_led
    name: "Switch state"
    id: status_led1
    output: led1
    internal: True

switch:
  - platform: gpio
    name: "电源开关"
    pin: GPIO13
    inverted: true
    id: relay
    restore_mode: RESTORE_DEFAULT_ON
    on_turn_on:
      - output.turn_on: led1
      - switch.turn_off: ina
      - switch.turn_on: inb
      - delay: 100ms
      - switch.turn_off: ina
      - switch.turn_off: inb
      - delay: 100ms
    on_turn_off:
      - output.turn_off: led1
      - switch.turn_on: ina
      - switch.turn_off: inb
      - delay: 100ms
      - switch.turn_off: ina
      - switch.turn_off: inb
      - delay: 100ms
    
  - platform: gpio
    pin: GPIO4
    id: ina
  - platform: gpio
    pin: GPIO5
    id: inb

interval:
  - interval: 5s
    then:
      if:
        condition:
          api.connected:
        then:
          if:
            condition:
              switch.is_on: relay
            then:
              - output.turn_on: led1
            else:
              - output.turn_off: led1


这样确实是可以了。

请问还有更好的实现办法吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-9-15 04:49 , Processed in 0.048350 second(s), 11 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表