Set template switch state in main.cpp

Hi,

I’m trying to set the state of a template switch in the main.cpp, but i’m not certain how to do this the correct way?

Main.cpp switch declaration:

  // switch.template:
  //   platform: template
  //   name: Input IO
  //   id: Input_IO
  //   internal: true
  //   optimistic: true
  //   disabled_by_default: false
  //   assumed_state: false
  //   restore_state: false
  Input_IO = new template_::TemplateSwitch();
  Input_IO->set_component_source("template.switch");
  App.register_component(Input_IO);
  App.register_switch(Input_IO);
  Input_IO->set_name("Input IO");
  Input_IO->set_disabled_by_default(false);
  Input_IO->set_internal(true);
  Input_IO->set_optimistic(true);
  Input_IO->set_assumed_state(false);
  Input_IO->set_restore_state(false);

I wan’t to set the state of the template switch ‘Input_IO’ the following way in my main.cpp :

// Check if Temperature Set message from Toon, then kill manualboiler process
  f = makeFloat (DataValue);
  if (Input_IO->state == true && MsgType == WRITE_DATA && DataId == CONTROL_SETPOINT && (f > Input_Temp->state))
  {  
    Input_IO = false;

  }

However when compiling I get the following error/warning:

src/main.cpp: In function 'void processRequest(long unsigned int, OpenThermResponseStatus)':
src/main.cpp:289:14: warning: converting 'false' to pointer type 'esphome::template_::TemplateSwitch*' [-Wconversion-null]
     Input_IO = false;

Can somebody give me a hint/clue in the right direction? I’m unable to find any(clear) documentation regarding how to set switch/number template state directly in main.cpp ( instead of doing trough normal yaml procedure )

Thanks,
Bart

This post has been around for quite some time, but here is my limited knowledge explanation. Every time you install the Esphome YAML file, the main.cpp file is scripted out in the build folder and installed by PlatformIO. You cannot intercept the installation process and change the scripted C++ code in main.cpp. You have to deal with this in your YAML file or custom component.

You may be able to work with a .bin file and some other form of installation. In addition, perhaps you could copy everything generated by the YAML file installation process into Visual Studio PlatformIO and install from there. HACK AWAY!!!