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

 找回密码
 立即注册
查看: 21739|回复: 20

[插件集成] 通过位操作完美控制云米浴霸

[复制链接]

1

主题

45

帖子

767

积分

高级会员

Rank: 4

积分
767
金钱
722
HASS币
0
发表于 2021-4-4 15:27:31 | 显示全部楼层 |阅读模式
参考了121665460的配置,通过位操作和template switch重写了一遍,不需要用automation了
sensor:
  - platform: xiaomi_miio_raw
    name: yunmi_bat_heater
    host: 192.168.0.237
    token: xxx
    default_properties:
      - power
      - humidity
      - temp
      - switch_state
      - wind_tem
      - off_time
  - platform: template
    sensors:
      bhwindtem:
        friendly_name: '设定温度'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','wind_tem') }}"
        unit_of_measurement: "°C"
      bhofftime:
        friendly_name: '延时关机时间'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','off_time') }}"
      bhhumidity:
        friendly_name: '浴霸湿度'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','humidity') }}"
        unit_of_measurement: "%"
      bhtemp:
        friendly_name: '浴霸温度'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','temp') }}"
        unit_of_measurement: "°C"
      bhstatus:
        friendly_name: '浴霸状态'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','switch_state') }}"

switch:
  - platform: template
    switches:
      bh_ventilate:
        friendly_name: '换气'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int|bitwise_and(4) > 0 }}"
        turn_on:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: ["{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int + 4 }}"]
        turn_off:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: >
                [{% if state_attr('sensor.yunmi_bat_heater','switch_state')|int - 4 > 0 %} {{ state_attr('sensor.yunmi_bat_heater','switch_state')|int - 4 }} {% else %} 1 {% endif %}]
      bh_lamp:
        friendly_name: '卫生间灯'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int|bitwise_and(16) > 0 }}"
        turn_on:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: ["{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int + 16 }}"]
        turn_off:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: >
                [{% if state_attr('sensor.yunmi_bat_heater','switch_state')|int - 16 > 0 %} {{ state_attr('sensor.yunmi_bat_heater','switch_state')|int - 16 }} {% else %} 1 {% endif %}]
      bh_customize:
        friendly_name: '自定义'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int|bitwise_and(256) > 0 }}"
        turn_on:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: ["{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int + 256 }}"]
        turn_off:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: >
                [{% if state_attr('sensor.yunmi_bat_heater','switch_state')|int - 256 > 0 %} {{ state_attr('sensor.yunmi_bat_heater','switch_state')|int - 256 }} {% else %} 1 {% endif %}]
      bh_heater:
        friendly_name: '暖风'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int|bitwise_and(4096) > 0 }}"
        turn_on:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: ["{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int + 4096 }}"]
        turn_off:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: >
                [{% if state_attr('sensor.yunmi_bat_heater','switch_state')|int - 4096 > 0 %} {{ state_attr('sensor.yunmi_bat_heater','switch_state')|int - 4096 }} {% else %} 1 {% endif %}]
      bh_blow:
        friendly_name: '自然风'
        value_template: "{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int|bitwise_and(16384) > 0 }}"
        turn_on:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: ["{{ state_attr('sensor.yunmi_bat_heater','switch_state')|int + 16384 }}"]
        turn_off:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: >
                [{% if state_attr('sensor.yunmi_bat_heater','switch_state')|int - 16384 > 0 %} {{ state_attr('sensor.yunmi_bat_heater','switch_state')|int - 16384 }} {% else %} 1 {% endif %}]
      bh_idle:
        friendly_name: '待机'
        turn_on:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: [1]
        turn_off:
          service: xiaomi_miio_raw.sensor_raw_command
          data_template:
            entity_id: sensor.yunmi_bat_heater
            method: set_switch
            params: [1]


评分

参与人数 2金钱 +30 收起 理由
+ 20 厉害了word楼主!
wormshow + 10 感谢楼主分享!

查看全部评分

回复

使用道具 举报

4

主题

69

帖子

473

积分

中级会员

Rank: 3Rank: 3

积分
473
金钱
404
HASS币
0
发表于 2021-4-5 21:16:10 | 显示全部楼层
大神帮忙看下我这个什么情况。

                               
登录/注册后可看大图

回复

使用道具 举报

1

主题

45

帖子

767

积分

高级会员

Rank: 4

积分
767
金钱
722
HASS币
0
 楼主| 发表于 2021-4-6 11:25:27 | 显示全部楼层
mr.chenwu 发表于 2021-4-5 21:16
大神帮忙看下我这个什么情况。

post一下日志
回复

使用道具 举报

4

主题

69

帖子

473

积分

中级会员

Rank: 3Rank: 3

积分
473
金钱
404
HASS币
0
发表于 2021-4-6 15:17:28 | 显示全部楼层

操作不能联动获取状态,控制倒是可以的,感谢您的回复

显示这个

显示这个
[140631873476304] string indices must be integers
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 141, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1488, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1523, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 642, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 681, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 679, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/template/switch.py", line 187, in async_turn_on
    await self._on_script.async_run(context=self._context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1041, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 254, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 262, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 463, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1488, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1523, in _execute_service
    await handler.job.target(service_call)
  File "/config/custom_components/xiaomi_miio_raw/sensor.py", line 132, in async_service_handler
    yield from getattr(device, method["method"])(**params)
  File "/config/custom_components/xiaomi_miio_raw/sensor.py", line 338, in async_command
    await self._try_command(
  File "/config/custom_components/xiaomi_miio_raw/sensor.py", line 240, in _try_command
    return result and (result[0] == "ok" or result[0]["code"] == 0)
TypeError: string indices must be integers

回复

使用道具 举报

6

主题

150

帖子

1782

积分

金牌会员

Rank: 6Rank: 6

积分
1782
金钱
1632
HASS币
0
发表于 2021-4-6 16:01:23 | 显示全部楼层
自定义可以反馈状态,不能控制,其它开关可以控制但是每次都报错:

调用服务 switch/turn_off失败。string indices must be integers
回复

使用道具 举报

1

主题

45

帖子

767

积分

高级会员

Rank: 4

积分
767
金钱
722
HASS币
0
 楼主| 发表于 2021-4-6 17:10:32 | 显示全部楼层
mr.chenwu 发表于 2021-4-6 15:17
操作不能联动获取状态,控制倒是可以的,感谢您的回复[140631873476304] string indices must be integer ...

File "/config/custom_components/xiaomi_miio_raw/sensor.py", line 240, in _try_command
    return result and (result[0] == "ok" or result[0]["code"] == 0)

把这行的ok改成大写
回复

使用道具 举报

1

主题

45

帖子

767

积分

高级会员

Rank: 4

积分
767
金钱
722
HASS币
0
 楼主| 发表于 2021-4-6 17:12:00 | 显示全部楼层
hanjun188 发表于 2021-4-6 16:01
自定义可以反馈状态,不能控制,其它开关可以控制但是每次都报错:

调用服务 switch/turn_off失败。string ...

试一下六楼的解决方案
回复

使用道具 举报

4

主题

69

帖子

473

积分

中级会员

Rank: 3Rank: 3

积分
473
金钱
404
HASS币
0
发表于 2021-4-6 18:23:20 | 显示全部楼层
cpuahqvd 发表于 2021-4-6 17:12
试一下六楼的解决方案

感谢大神。彻底搞定了。完美完美。这个问困扰我一个月了,之前论坛的方法我智能显示状态完全不能控制,刚刚时下完全可以了。
回复

使用道具 举报

4

主题

69

帖子

473

积分

中级会员

Rank: 3Rank: 3

积分
473
金钱
404
HASS币
0
发表于 2021-4-6 18:24:36 | 显示全部楼层
cpuahqvd 发表于 2021-4-6 17:12
试一下六楼的解决方案

刚刚看了下  状态还是显示1234 有点点小问题,之前可以显示换气什么的
回复

使用道具 举报

1

主题

45

帖子

767

积分

高级会员

Rank: 4

积分
767
金钱
722
HASS币
0
 楼主| 发表于 2021-4-6 18:31:39 | 显示全部楼层
mr.chenwu 发表于 2021-4-6 18:24
刚刚看了下  状态还是显示1234 有点点小问题,之前可以显示换气什么的

状态那个sensor我去掉了没有用,因为switch的value_template已经能把当前状态准确显示出来了
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-27 02:18 , Processed in 0.062324 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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