Rest_command url variable? not working?

Hi there,

i have a thermostat wich i control over rest_commands.
Set temperature with a hex number like 22.5 degr.:
22.5 x 2 = 45 = 2D(hex)

http://192.168.xxx.xx/command…=adressThermostat+setTemp+HEXNUMBER”

Now i want to send the temperature via a automation, that is triggered when i change a input_number slider (only 0.5 steps), that number is going through a helper sensor template wich looks like this:

  • name: “Input temperatur Regler Wohnzimmer”
    state: >
    {% set tempFloat = (states(‘input_number.temperaturregler’)) | float %}
    {% set tempInt = (tempFloat*2) | int %}
    {% set tempHex = ‘%0x’ % tempInt %}
    {{ tempHex }}

When i look at my entity now it shows me the correct hex number 2d, but my thermostat change his
temp to 103.0 degr.

When i go to the developer tools and try it with the service call:

service: rest_command.temp_set
data:
url: 2d

it works totally fine.

my rest_command looks like this:
temp_set:
url: “http://192.168.XX.XX/command?=HM&data=adressThermo111{{url}}”

My thought was that when i send it in the developer service call it is an int? and when it
is send through the template it changes its value string? etc… ?

Thanks for your help :slight_smile:

If I understand you right template sensor and rest command work fine but automation doesn’t.
So it would be helpful to see yaml-code of your automation, too. (Please use blockquote to post the code.)

Hi,

no i think the automation does work, but i think that the format does not work… hmm hard to explain.

  • id: ‘1673824094158’
    alias: Temperatur ändern Wohnzimmer
    description: ‘’
    trigger:
  • platform: state
    entity_id:
    - input_number.temperaturregler
    id: temperaturreglerChange
    condition:
    action:
  • choose:
    - conditions:
    - condition: trigger
    id: temperaturreglerChange
    sequence:
    - service: rest_command.temp_set
    data:
    hex: sensor.input_temperatur_regler_wohnzimmer
    mode: single
  • name: “Input temperatur Regler Wohnzimmer”
    state: >
    {% set tempFloat = (states(‘input_number.temperaturregler’)) | float %}
    {% set tempInt = (tempFloat*2) | int %}
    {% set tempHex = (‘%0x’ | format(tempInt|int)) %}
    {{ tempHex }}

i think my error is somewhere here in the url:

temp_set:
url: “http://192.168.170.XX/command?XC_USER=user&XC_PASS=XXXXXXX!&XC_FNC=SendSC&type=HM&data=6BE3120111{{hex}}”

the data is: adress of the device: 6BE31201 + 11 for setTemp + Temp in HEX

It works totally fine if i send it via the Browser or i test it in the dev service and temp in hex as a example
21 degres = 2A(HEX) (they calculate 21*2 = 42 = 0x2A)
so the url looks like: http:…data=6BE31201112A

And if i change my slider to 21 deg my helper sensor change his value correctly to 2a
but my thermostat get something else :slight_smile: so i think the error is somewhere in the formatting
of this “variable”? and how it is send to this string?

.“…6BE3120111{{hex}}”

Hi,

thank you, yes this was what i was thinking too in some way :slight_smile:
I tried it, but it dont work.
Then i try it in the template editor i get this error:

ValueError: Template error: int got invalid input ‘2a’ when rendering template '{## Imitate available variables: ##}
{% set input = 21.0 %}

{% set tempFloat = input | float %}
{% set tempInt = int(tempFloat*2) %}
{% set hex = int(‘%0x’ % tempInt) %}

{{ tempFloat }}
{{ tempInt }}
{{ hex }}’ but no default was specified

My url looks like this:

url: “http://192.168.170.XX/command?XC_USER=user&XC_PASS=123456789&XC_FNC=SendSC&type=HM&data=6BE3120111{{hex}}”

int and float should have a default value which comes into play when your hex calculation doesn’t work.

Also, please format code correctly using the </> button. There are a few “smart quotes” in the provided code which will not work in templates — check that these are actually “normal” quotes:

image
image

Here’s a nice way to get the hex value for twice the input value:

{% set input = 22.5 %}
{% set in2 = (input*2)|int(0) %}
{% set h = "0123456789ABCDEF" %}
{% set hex = h[in2//16]~h[in2%16] %}
{{ hex }}

Thanks for your help,
ok but i dont want that because in the example 21 degre it is hex 2A, and i need 2A and not 0

That’s because your template wasn’t working. Refresh this page and you’ll see my solution in the previous post.

Oh yes ok i will try :slight_smile:
thank you so much

Still dont work it is soooo disappointing :frowning:

Please post the complete rest_command code, properly formatted with the </> button, and similarly the code for the sensor that’s showing 103°C.

rest_command:
  temp_set:
    url: "http://192.168.170.123/command?XC_USER=user&XC_PASS=123456789&XC_FNC=SendSC&type=HM&data=6BE3120111{{hex}}" 
  - platform: rest
    resource: http://192.168.170.26/command?XC_USER=user&XC_PASS=123456789&XC_FNC=GetStates
    name: "Thermostat Wohnzimmer"
    unit_of_measurement: '°C'
    scan_interval: 10
    value_template: >-
        {% set response = value[8:] | from_json %}
        {% set hexString = response[0].state[4:8] %}
        {{ hexString | int(hexString,16) / 10 }}

And thats the automation:

- id: '1673824094158'
  alias: Temperatur ändern Wohnzimmer
  description: ''
  trigger:
  - platform: state
    entity_id:
    - input_number.temperaturregler
    id: temperaturreglerChange
  condition: []
  action:
  - choose:
    - conditions:
      - condition: trigger
        id: temperaturreglerChange
      sequence:
      - service: rest_command.temp_set
        data:
          hex: sensor.input_temperatur_regler_wohnzimmer
  mode: single

And the status:

This is the problem. Try the following, which assumes you can use a template in the service call:

      - service: rest_command.temp_set
        data:
          hex: "{{ states('sensor.input_temperatur_regler_wohnzimmer') }}"

If that fails, you could put the calculation in the rest_command itself:

rest_command:
  temp_set:
    url: "http://192.168.170.123/command?XC_USER=user&XC_PASS=123456789&XC_FNC=SendSC&type=HM&data=6BE3120111{{ states('sensor.input_temperatur_regler_wohnzimmer') }}" 

…and call it without the hex parameter.

Ahhhhhh i love YOU!!! :1st_place_medal:
Thank you it worked!!! can i get you a beer :beer: ?
:hugs: :hugs: :hugs:

Thanks so much really!!

1 Like

Just mark my post as Solution and I’ll be happy. :sunglasses: