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

标题: 自制超声波水池液位(tasmota SR-04),超声波数值模板如何c... [打印本页]

作者: wfx    时间: 2021-12-24 11:16
标题: 自制超声波水池液位(tasmota SR-04),超声波数值模板如何c...
本帖最后由 wfx 于 2021-12-24 15:09 编辑

水池高度为200cm(超声波SR-04满量程),假如传感器SR-04测得当前的水位为30cm,实际水深为200cm-30cm=170cm。使用模板怎样把这个表达式写出来?
http://sidweb.nl/tasmota/release-10.1.0/
[attach]34247[/attach]

[attach]34248[/attach]




作者: wfx    时间: 2021-12-24 11:36
sensor:
  - platform: template
    sensors:
      pv_current_change:
        unit_of_measurement: cm
        device_class: current
        friendly_name: '水池水位'
        value_template: >
          {% set state = states('sensor.tasmota_sr04_distance') %}{{ (state | float / 100) | round(2)}}  这个表达式怎么转换加减数?
作者: pauillac21    时间: 2021-12-24 12:11
没太明白,你是想要 {{( 2-(state | float / 100) )| round(2)}}
作者: wfx    时间: 2021-12-24 12:16
pauillac21 发表于 2021-12-24 12:11
没太明白,你是想要 {{( 2-(state | float / 100) )| round(2)}}

{% set state = states('sensor.tasmota_sr04_distance') %}{{ (state | float / 100) | round(2)}}这个表达式我是复制别人的,我的水位传感器是sensor.tasmota_sr04_distance
作者: wfx    时间: 2021-12-24 12:21
{{( 200-(state('sensor.tasmota_sr04_distance') | float / 100) )| round(2)}} 这样也不对!
作者: pauillac21    时间: 2021-12-24 12:24
你直接用: {{ ( 2-(states('sensor.tasmota_sr04_distance')  | float / 100) )| round(2) }} ,前面那个2就是你水池深度2m,是这意思么?
作者: pauillac21    时间: 2021-12-24 12:26
wfx 发表于 2021-12-24 12:21
{{( 200-(state('sensor.tasmota_sr04_distance') | float / 100) )| round(2)}} 这样也不对!

你上面/100,我猜传感器是给的CM,转换成了m,你把/100去掉,或者前面200改成2,单位就是CM或者M
作者: wfx    时间: 2021-12-24 12:36
pauillac21 发表于 2021-12-24 12:24
你直接用: {{ ( 2-(states('sensor.tasmota_sr04_distance')  | float / 100) )| round(2) }} ,前面那个2 ...

表达式的内容我不懂,呵呵,我是乱写的。sensor.tasmota_sr04_distance这个是我的传感器实体,假如sensor.tasmota_sr04_distance=30cm,水池高度为200cm(sensor.tasmota_sr04_distance的满量程),实际水位怎么写?也就是200cm-30cm=170cm
作者: pauillac21    时间: 2021-12-24 12:43
{{ (200-(state('sensor.tasmota_sr04_distance') )| round(2)}}
这么写把,如果你的传感器给出的是厘米,这个应该给出实际水位厘米的结果,保留了2位小数。如果不想要小数,就把round(2)改成round(0)
作者: wfx    时间: 2021-12-24 12:47
C:\Users\Administrator\Desktop\100.jpg
作者: pauillac21    时间: 2021-12-24 12:48
刚才多写了个括号,用这个:
{{ (200-state('sensor.tasmota_sr04_distance') )| round(2)}}

作者: wfx    时间: 2021-12-24 13:04
这个表达式的结果是这样的
作者: wfx    时间: 2021-12-24 13:09
报错了!
作者: wfx    时间: 2021-12-24 13:15
不可用?
作者: wfx    时间: 2021-12-24 13:17
2021-12-24 13:14:43 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'state' is undefined when rendering '{{ (200-state('sensor.tasmota_sr04_distance') )| round(2)}}'
2021-12-24 13:15:43 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'state' is undefined when rendering '{{ (200-state('sensor.tasmota_sr04_distance') )| round(2)}}'
作者: pauillac21    时间: 2021-12-24 13:23
应该是states,不是state
作者: wfx    时间: 2021-12-24 13:28
还是不可用!
作者: pauillac21    时间: 2021-12-24 13:48
wfx 发表于 2021-12-24 13:28
还是不可用!

看不出来还有什么问题啊。。。
作者: wfx    时间: 2021-12-24 14:37
2021-12-24 13:26:46 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for -: 'int' and 'str'') while processing template 'Template("{{ (200-states('sensor.tasmota_sr04_distance') )| round(2)}}")' for attribute '_attr_native_value' in entity 'sensor.pv_current_change'

作者: wfx    时间: 2021-12-24 14:49
{{ (200-states('sensor.tasmota_sr04_distance')|int )| round(2)}}这样子就可以了,谢谢您!

作者: Tao173    时间: 2021-12-24 15:32
esphome可以直接计算好然后发送到HA,不需要自己写模版
作者: wfx    时间: 2021-12-24 15:54
Tao173 发表于 2021-12-24 15:32
esphome可以直接计算好然后发送到HA,不需要自己写模版

esphome也要写配置文件,tasmota比较适合我这小白,但模板也不会写!想这出自己满意的效果就需要各位大佬指点了!像这个超声波水位实际测得的是液位到传感器的距离,距离传感器越近说明水位越深。如果不使用模板转换的话这个数值很尴尬,也不直观!所以希望各位大佬多多指导。

作者: zkxa    时间: 2021-12-24 15:54
怎么不用ESPHOME   我用ESPHOME搞过液位的。。
作者: wfx    时间: 2021-12-24 15:55
实测和模板对比,呵呵呵
作者: wfx    时间: 2021-12-24 16:08
恳请各位路过的大佬再帮忙,已知水池长度为6m,宽度为3m,高度为2m。在此模板{{ (200-states('sensor.tasmota_sr04_distance')|float)| round(2)}}基础上换算出水池容量。谢谢大家




欢迎光临 『瀚思彼岸』» 智能家居技术论坛 (https://bbs.hassbian.com/) Powered by Discuz! X3.5