『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1809|回复: 2

[基础教程] 新手的第一条blueprint

[复制链接]

20

主题

216

帖子

1458

积分

论坛DIY达人

积分
1458
金钱
1217
HASS币
100
发表于 2023-6-17 21:38:51 | 显示全部楼层 |阅读模式
本帖最后由 zro22 于 2023-6-17 22:04 编辑

是这样的,我买了个万能遥控器,包含了红外,蓝牙,射频433和315。对,我也买了个这个帖子说的几百块的遥控器
https://bbs.hassbian.com/thread-14172-1-1.html
然后用ESP32做了一个RF桥,负责接收射频信号然后把码转给MQTT。
为了方便写自动化,创建了一个blueprint,具体内容就是侦测MQTT的topic,判断topic里面的码,开关对应的灯。blueprint写好之后,配置自动化,只需要选择码加上对应的设备就可以了。
算是学习了一下blueprint的编写,中间还趟了几个坑,或者不叫坑,是摸索的过程。
整个blueprint如下(这个是单码控制单/多灯,我实际用是四码控制四/多灯)
东西一旦写好实际很简单,大家一看就懂,但是写的过程很痛苦,一点点看示例,debug。

比如最开始value_a没有指定类型,结果跟踪debug发现 playload是 11111 value_a是'11111'导致判断为0不执行。
还有开始只写了switch.turn_on 发现无法turn_on light,所以加了一个light_turn_on。
发在论坛权当是一个备份吧,不然过几天又忘了怎么写。
我的RF码是aabb0或者aabb1 0代表关 1代表开,如果你的码不是这样,就需要分别定义开和关的码。
blueprint:
  name: mqtt topic to trigger switch or light 4 cnt
  description: create 4 rf bridge or other mqtt topic value to control light or switch
  domain: automation
  input:
    mqtttopic:
    value_a:
      selector:
        number:
    light_a:
      selector:
        entity:
          domain:
          - light
          - switch
          multiple: true    
trigger:
  platform: mqtt
  topic: !input mqtttopic
action:
- variables:
    payload: "{{ trigger.payload }}"
    value_a: !input value_a 
- choose:
  - conditions:
    - "{{ payload == value_a*10 }}"
    sequence: 
      - service: switch.turn_off
        data:
          entity_id: !input light_a
      - service: light.turn_off
        data:
          entity_id: !input light_a    
  - conditions:
    - "{{ payload == value_a*10+1 }}"
    sequence: 
      - service: switch.turn_on
        data:
          entity_id: !input light_a    
      - service: light.turn_on
        data:
          entity_id: !input light_a        
如下是四路的
blueprint:
  name: mqtt topic to trigger switch or light 4 cnt
  description: create 4 rf bridge or other mqtt topic value to control light or switch
  domain: automation
  input:
    mqtttopic:
    value_a:
      selector:
        number:
    value_b:
      selector:
        number:
    value_c:
      selector:
        number:
    value_d:
      selector:
        number:        
    light_a:
      selector:
        entity:
          domain:
          - light
          - switch
          multiple: true    
    light_b:
      selector:
        entity:
          domain:
          - light
          - switch
          multiple: true    
    light_c:
      selector:
        entity:
          domain:
          - light
          - switch
          multiple: true    
    light_d:
      selector:
        entity:
          domain:
          - light
          - switch
          multiple: true    
          
trigger:
  platform: mqtt
  topic: !input mqtttopic
action:
- variables:
    payload: "{{ trigger.payload }}"
    value_a: !input value_a 
    value_b: !input value_b 
    value_c: !input value_c 
    value_d: !input value_d 
- choose:
  - conditions:
    - "{{ payload == value_a*10 }}"
    sequence: 
      - service: switch.turn_off
        data:
          entity_id: !input light_a
      - service: light.turn_off
        data:
          entity_id: !input light_a    
  - conditions:
    - "{{ payload == value_a*10+1 }}"
    sequence: 
      - service: switch.turn_on
        data:
          entity_id: !input light_a    
      - service: light.turn_on
        data:
          entity_id: !input light_a        
  - conditions:
    - "{{ payload == value_b*10 }}"
    sequence: 
      - service: switch.turn_off
        data:
          entity_id: !input light_b
      - service: light.turn_off
        data:
          entity_id: !input light_b    
  - conditions:
    - "{{ payload == value_b*10+1 }}"
    sequence: 
      - service: switch.turn_on
        data:
          entity_id: !input light_b    
      - service: light.turn_on
        data:
          entity_id: !input light_b        
  - conditions:
    - "{{ payload == value_c*10 }}"
    sequence: 
      - service: switch.turn_off
        data:
          entity_id: !input light_c
      - service: light.turn_off
        data:
          entity_id: !input light_c    
  - conditions:
    - "{{ payload == value_c*10+1 }}"
    sequence: 
      - service: switch.turn_on
        data:
          entity_id: !input light_c
      - service: light.turn_on
        data:
          entity_id: !input light_c        
  - conditions:
    - "{{ payload == value_d*10 }}"
    sequence: 
      - service: switch.turn_off
        data:
          entity_id: !input light_d
      - service: light.turn_off
        data:
          entity_id: !input light_d    
  - conditions:
    - "{{ payload == value_d*10+1 }}"
    sequence: 
      - service: switch.turn_on
        data:
          entity_id: !input light_d    
      - service: light.turn_on
        data:
          entity_id: !input light_d        
          




评分

参与人数 1金钱 +10 收起 理由
隔壁的王叔叔 + 10 高手,这是高手!

查看全部评分

回复

使用道具 举报

10

主题

1332

帖子

4254

积分

论坛元老

Rank: 8Rank: 8

积分
4254
金钱
2922
HASS币
0
发表于 2023-6-17 22:34:38 | 显示全部楼层
感谢分享。
回复

使用道具 举报

6

主题

153

帖子

578

积分

高级会员

Rank: 4

积分
578
金钱
425
HASS币
0
发表于 2024-3-21 16:25:20 | 显示全部楼层
什么是blueprint,代码写在哪里,干什么用的,小白一枚,不懂就问
总为浮云能避日,长安不见使人愁
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-25 14:31 , Processed in 0.095007 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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